我正在使用以下代码从 json 获取版本
网络服务.java
public void getSingleResponse(final String url) {
class task extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String resp;
resp = "";
HttpClient httpClient = new DefaultHttpClient();
try {
Log.d("URL", "" + params[0]);
HttpResponse response = httpClient.execute(new HttpGet(
params[0]));
HttpEntity entity = response.getEntity();
Log.d("Entity ", "" + entity);
String responseString = EntityUtils.toString(entity,
"UTF-8");
resp = responseString;
GlobalVar.Version = resp;
Log.v("Global Version", "" + GlobalVar.Version);
} catch (Exception e) {
// TODO: handle exception
Log.v("HTTP:::::", "error" + e.getCause());
}
return GlobalVar.Version;
}
}
new task().execute(url);
}
MainActivity.java
public static String getMarketVersion() throws JSONException {
String version = "";
try {
webservice ws = new webservice();
String serverUrl = SERVER_URL + "/GetVersion";
Log.i(TAG, "Server URL" + serverUrl);
ws.getSingleResponse(serverUrl);
String response = GlobalVar.Version;
Log.v("Response :: ", "Version ::" + response);
JSONArray jArry = new JSONArray(response);
JSONObject jObj = jArry.getJSONObject(0);
version = jObj.getString("Version");
Log.v("Parsing ", "Version :: " + version);
return version;
} catch (Exception e) {
e.printStackTrace();
}
return version;
}
当我运行应用程序时,我得到了版本空白,请参阅下面的日志
07-24 12:27:42.309: V/Response ::(4268): Version ::
07-24 12:27:43.389: V/Global Version(4268): [{"Version":"1.1.3"}]
但是当我调试应用程序时,我能够得到响应
07-24 12:06:01.699: V/Global Version(3122): [{"Version":"1.1.3"}]
07-24 12:06:07.349: V/Response ::(3122): Version ::[{"Version":"1.1.3"}]
07-24 12:08:12.019: V/Parsing(3122): Version :: 1.1.3
我不知道发生了什么提前谢谢