程序停留在getResponseCode()

时间:2015-09-30 10:24:14

标签: android httpurlconnection

我正在编写一个Android应用程序,我需要从一个url解析json。 程序在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);
            }
        }
    }

它适用于除一个以外的所有其他网址。 知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

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