-1
我正在寫一個android應用程序,我需要從url解析json。 程序在getResponseCode()後凍結,不顯示任何錯誤。 調試器顯示「應用程序正在運行」。但程序卡住了。我的代碼是:程序卡住在getResponseCode()
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setConnectTimeout(1000);
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.connect();
int status = c.getResponseCode();
System.out.println(status);
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
String json = sb.toString();
try {
jArray = new JSONArray(json);
jObj = new JSONObject[jArray.length()];
//Get each object from JSON Array
for(int i=0;i<jArray.length();i++) {
jObj[i] = jArray.getJSONObject(i);
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}catch (Exception e){
Log.e("Something wrong",""+e.toString());
}
}
} catch (MalformedURLException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
它適用於除一個之外的所有其他url。 任何想法,爲什麼這可能會發生?
r你在後臺調用 – Pavan
我有它在AsyncTask – pikachu
所以你的應用程序沒有響應,當你打網絡電話 – Pavan