2015-09-30 57 views
-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。 任何想法,为什么这可能会发生?

+0

r你在后台调用 – Pavan

+0

我有它在AsyncTask – pikachu

+0

所以你的应用程序没有响应,当你打网络电话 – Pavan

回答

0

在这种情况下,这是一个服务器问题。