【问题标题】:HttpUrlConnection working on api level < 11, not working on api level >11 on androidHttpUrlConnection 在 api 级别 < 11 上工作,在 android 上的 api 级别 >11 上不工作
【发布时间】:2014-01-01 06:53:54
【问题描述】:

首先我有两件事需要你注意:

1) 我在 api 级别低于 11 的手机和相当高的手机(如 4.3 或 4.4)上尝试了此代码。高 api 级别的手机无法使此代码与其他代码不同。 2)我在另一个页面中有几乎完全相同的代码,使用它,它适用于所有api,只有我要向你展示的那个不起作用。 感谢您的收听。

这是我建立连接的方法:

    public static StringBuilder httpGetConnection (String urlStr, Context dialogCtx) {
    final String noServ = "Server Error";
    final String noServErr = "Server is unavailable";
    StringBuilder err= new StringBuilder();
    err.append("Error!");

    HttpURLConnection conn = null;
    BufferedReader rd = null;
    StringBuilder sb = null;
    try {
        URL url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(6000);
        conn.setReadTimeout(10000);
        if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 299) {
            // Buffer the result into a string
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            } //string builder dolduruldu
            rd.close();

            conn.disconnect();

        }
        else {
            AlertDialogDisp(dialogCtx, noServ, noServHata);
        }
    }
    catch (Exception e) {
        return err;
    }
    finally {
        if (rd != null) {
            try {
                rd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            conn.disconnect();
        }
    }
    return sb;
}

这是我“使用”该方法的代码:

  try {
  JSONTokener neuTokener = new JSONTokener(Sources.httpGetConnection (UrlStr, getActivity()).toString());
        JSONArray neuArray=new JSONArray(neuTokener);

        for(int i=0; i<(neuArray.length()); i++)
        {
            JSONObject json_obj_neu_sip = neuArray.getJSONObject(i);

            wartSip = json_obj_neu_sip.getString("mit");
            wartSip2 = json_obj_neu_sip.getString("tua");
            akkSip = json_obj_neu_sip.getString("ptar");
            ajjSip = json_obj_neu_sip.getString("ptar2");
        }
        ...

我在片段中使用此代码,它使用该方法。当我尝试启动它时,neuTokener 的值为“错误!”。我的方法中的StringBuilder 当它掉下来时。但我不知道它为什么会抓住这个东西,我在不同的地方使用它,没有问题。 非常感谢您的帮助,在此先感谢。

【问题讨论】:

  • 你在主线程上调用httpGetConnection 吗?
  • Rly 另一个 NetworkOnMainThreadException??
  • @A.S.是的,就像每隔一天一样。这不像是有一个搜索引擎可以搜索错误,这是被抛出的。
  • @A.S.好吧我不知道这个,我是新手。对不起,我猜?
  • 错误一定在logcat中,与哪个类无关

标签: java android json httpurlconnection arrays


【解决方案1】:

您需要通过AsyncTask在后台线程中实现所有Network Calls
这是一个虚拟模板,您可以根据需要进行修改:

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        //Send your HTTP REQUESTS HERE.
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        //UPDATE YOUR UI HERE AFTER RETRIEVING DATA FROM HTTP REQUEST
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}
}

你需要调用这个 AsyncTask,像这样:new LongOperation().execute();
我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可能遇到 NetworkOnMainThreadException 连接错误。

    所以最好的方法是使用 AsyncTask

    更多详情请联系this

    【讨论】:

      【解决方案3】:

      一个非常常见的错误 - 是在主 UI 线程中尝试调用 httpGetURLConnection,因为这是一个非常繁重的操作,并且当您使用 httpGetURLConnection 时,UI 不能“冻结”,所以为什么在新的 android 版本中(在honeyComb之后)它完全被弃用了。您应该使用 asyncTask 来处理 httpGetURLConnection。

      【讨论】:

      • 谢谢,这正是我想知道的,而不是我有多么错误和愚蠢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多