我有這段代碼,它返回Json
對象從URL。 它在froyo
中正常工作,但相同的代碼在Jelly Beans
中不起作用。代碼不工作在果凍豆
我的代碼有問題嗎?
請幫助我,這裏是我的代碼
public static JSONObject getJSONfromURL(String url) {
// initialize
InputStream is = null;
String result = "";
JSONObject jArray = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
這裏是
11-01 11:01:48.437: E/log_tag(1044): Error in http connection android.os.NetworkOnMainThreadException 11-01 11:01:48.437: E/log_tag(1044): Error converting result java.lang.NullPointerException 11-01 11:01:48.437: E/log_tag(1044): Error parsing data org.json.JSONException: End of input at character 0 of
去沒有任何錯誤?如果是的話請張貼。是的,你正在做網絡相關的任務,所以請確保你沒有在主線程中執行此操作請參閱[this](http://android-developers.blogspot.in/2009/05/painless-threading.html) – juned
意思是「不工作」? –
究竟是什麼問題只看到logcat是否有錯誤? – Ravi