Android 登陆功能的实现(访问WebServices 解析返回的JSON结果)
1、 WebServices返回的JSON结果
{ "Code": 1, "Result": [{ "ID": "2", "UserName": "123", "UserPwd": "123", "IsEnable": "0" }] }
2、访问WEB服务代码
1 import android.os.Bundle; 2 import android.os.Message; 3 import android.util.Log; 4 import android.widget.Toast; 5 6 import com.google.gson.Gson; 7 import com.google.gson.JsonArray; 8 import com.google.gson.JsonElement; 9 import com.google.gson.JsonParser; 10 import com.google.gson.reflect.TypeToken; 11 12 13 import org.apache.http.HttpResponse; 14 import org.apache.http.client.entity.UrlEncodedFormEntity; 15 import org.apache.http.client.methods.HttpGet; 16 import org.apache.http.client.methods.HttpPost; 17 import org.apache.http.impl.client.DefaultHttpClient; 18 import org.apache.http.message.BasicNameValuePair; 19 import org.apache.http.protocol.HTTP; 20 import org.json.JSONArray; 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 import java.io.BufferedReader; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.io.InputStreamReader; 28 import java.util.ArrayList; 29 import java.util.List; 30 31 import static java.net.Proxy.Type.HTTP; 32 //判读账号、密码 33 public static String GetUsers(String code, String pwd) { 34 String result = ""; 35 InputStreamReader in = null; 36 try { 37 final String SERVER_URL = GlobalAppliaction.WebUrl + "LogIn"; // 定义需要获取的内容来源地址 38 HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求 39 40 List params = new ArrayList(); 41 params.add(new BasicNameValuePair("username", code)); // 添加必须的参数 42 params.add(new BasicNameValuePair("userpwd", pwd)); // 添加必须的参数 43 request.setEntity(new UrlEncodedFormEntity(params, org.apache.http.protocol.HTTP.UTF_8)); // 设置参数的编码 44 45 46 HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 发送请求并获取反馈 47 48 // 解析返回的内容 49 if (httpResponse.getStatusLine().getStatusCode() != 404) { 50 InputStream inputStream = httpResponse.getEntity().getContent(); 51 in = new InputStreamReader(inputStream); 52 BufferedReader bufferedReader = new BufferedReader(in); 53 54 StringBuffer strBuffer = new StringBuffer(); 55 String line = null; 56 while ((line = bufferedReader.readLine()) != null) { 57 strBuffer.append(line); 58 } 59 inputStream.close(); 60 bufferedReader.close(); 61 result = strBuffer.toString(); 62 String errCode = ""; 63 String strresult = ""; 64 JSONObject root = new JSONObject(result); 65 errCode = root.getString("Code"); 66 67 68 if (errCode.equals("1")) { 69 70 strresult = root.getString("Result"); 71 /* 72 JSONArray resultArray = new JSONArray(strresult);//解析result 73 JSONObject resultobj = resultArray.getJSONObject(0); 74 resultobj.getString("UserName"); 75 resultobj.getString("UserPwd"); 76 */ 77 78 result = strresult; 79 return result; 80 } else { 81 return "-1"; 82 } 83 84 85 } 86 87 88 } catch (IOException e) { 89 e.printStackTrace(); 90 Log.d("debug", e.toString()); 91 return "404"; 92 } catch (JSONException e) { 93 e.printStackTrace(); 94 Log.d("debug", e.toString()); 95 96 } finally { 97 98 if (in != null) { 99 try { 100 in.close(); 101 } catch (IOException e) { 102 e.printStackTrace(); 103 } 104 } 105 } 106 return result; 107 }
3、读取访问WEB服务结果
try { //启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI GetWebResultTask queryAddressTask = new GetWebResultTask(); //启动后台任务 queryAddressTask.execute(etname.getText().toString(), etpwd.getText().toString()); } catch (Exception ex) { }
public class GetWebResultTask extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { String GetResult = ""; try { GetResult = WebByHttp.GetUsers(params[0], params[1]); DialogBase.showmessage(LogIn.this, "GetResult:" + GetResult, "doInBackground"); } catch (Exception e) { e.printStackTrace(); } //将结果返回给onPostExecute方法 return GetResult; } @Override //此方法可以在主线程改变UI protected void onPostExecute(String result) { // 将WebService返回的结果显示在TextView中 tvresult.setText(result); try { if (result=="404") { DialogBase.showmessage(LogIn.this, "网络连接有误,请检查网络地址配置信息!", "确定"); return; } if (result=="-1") { DialogBase.showmessage(LogIn.this, "账号或密码有误!", "确定"); } else { Intent itent = new Intent(LogIn.this, MainActivity.class); startActivity(itent); } } catch (Exception e) { e.printStackTrace(); } } }