HttpURLConnection类的API级别&LT工作;在Android上11; 11,不工作的API级别&GT [英] HttpUrlConnection working on api level < 11, not working on api level >11 on android

查看:123
本文介绍了HttpURLConnection类的API级别&LT工作;在Android上11; 11,不工作的API级别&GT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我需要您的注意两件事情:

First of all I have two things that need your attention:

1)我试图与API级手机低于11,并且是pretty高,如4.3或4.4的手机这code。高API铲平手机无法使这个code的工作不像其他的。
2)我几乎确切code在另一页,使用它,它的工作对所有API,只有一个我要告诉你不工作。
谢谢你听。

1) I tried this code on phones with api level lower than 11 and phones that are pretty high, like 4.3 or 4.4. The high api leveled phones were unable to make this code work unlike the other ones. 2)I have almost the exact code in another page, using it, it's working on all apis, only the one I'm going to show you isn't working. Thank you for listening.

这是我的方法,使连接:

This is my method that makes the connection:

    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;
}

这是我的code的'使用'的方法:

And this is my code that 'uses' the method:

  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");
        }
        ...

我使用这个code,它使用的方法,在一个片段。当我尝试启动它,在 neuTokener 的值是错误!。从我的方法中的的StringBuilder 当它落在捕捉。但我不知道为什么它捕获的东西,我在不同的地方使用这个并没有什么问题。
我真的AP preciate的帮助下,先谢谢了。

I'm using this code, which uses the method, in a fragment. When I try to launch it, the neuTokener 's value is "Error!". The StringBuilder from my method when it falls to catch. But I have no idea why it catches the thing, I use this in different places and there is no problem. I'd really appreciate the help, thanks in advance.

推荐答案

您需要实现所有的网​​络电话在通过后台线程的AsyncTask

这是一个虚拟的模板,您可以根据自己的需要进行修改:

You need to implement all the Network Calls in background Thread via AsyncTask.
Here is a dummy template, you can modify it according to your needs:

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的,就像这样:新LongOperation()执行();

我希望这有助于。

You need to call this AsyncTask, like this: new LongOperation().execute();
I hope this helps.

这篇关于HttpURLConnection类的API级别&LT工作;在Android上11; 11,不工作的API级别&GT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆