Android中通过HttpPost、HttpResponse两个类,访问某一接口。
再使用JSONObject类,来从返回的json数据中解析出数据给Bean实体类。
不啰嗦,见方法块代码:
public static AboutModel getAboutMsg(Context ctx) {
String requestUrl = WXConstants.getAboutMsgURL;
try {
HttpPost request = new HttpPost(requestUrl);
HttpResponse httpResponse = new DefaultHttpClient()
.execute(request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String retSrc = EntityUtils.toString(httpResponse.getEntity());
JSONObject json = new JSONObject(retSrc);
String qqMsg = json.getString("qqmsg");
String url = json.getString("url");
AboutModel model = new AboutModel();
model.qqMsg = qqMsg;
model.companyUrl = url;
return model;
}
} catch (Exception e) {
// TODO Auto-generated catch block
ExceptionThrowUtil.ThrowException(ctx, e);
e.printStackTrace();
}
return null;
}
注意:网络访问要try-catch{}
注:解析的json如下
{"qqmsg":"客服QQ:111222333","url":"http://www.baidu.com"}