【问题标题】:why is AsyncTask doing deprecated lines ? in my code?为什么 AsyncTask 做不推荐使用的行?在我的代码中?
【发布时间】:2016-03-14 07:06:28
【问题描述】:

我正在做一个可以添加的 AsyncTask 如果我将我的 sdk 降低到 22。

在 sdk 23 上它根本不起作用,即使在 22 中,它也为我写了一些行。

我做错了什么? 或者如何更正它以便我可以在 sdk 23 上运行它?

@Override
    protected Boolean doInBackground(String... urls) {

        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);

                //this gets us the actors node and puts it in jarray veriable
                JSONArray jarray = jsono.getJSONArray("actors");

                for (int i = 0; i < jarray.length(); i++) {
                    //the object veriable will be the node in position i inside the "actors" node in the main json
                    JSONObject object = jarray.getJSONObject(i);


                    Log.d("myAppLog", "next one is: ");
                    Log.d("myAppLog", object.getString("name"));

                    actorClass actorClassVeriable = new actorClass();


                    actorClassVeriable.setName(object.getString("name"));
                    actorClassVeriable.setDescription(object.getString("description"));
                    actorClassVeriable.setDob(object.getString("dob"));
                    actorClassVeriable.setCountry(object.getString("country"));
                    actorClassVeriable.setHeight(object.getString("height"));
                    actorClassVeriable.setSpouse(object.getString("spouse"));
                    actorClassVeriable.setChildren(object.getString("children"));
                    actorClassVeriable.setImage(object.getString("image"));

                    actorsList.add(actorClassVeriable);
                }
                return true;
            }

            //------------------>>

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}
  • 它记录了 > HttpGet、HttpClient、HttpResponse 和 HttpEntity

【问题讨论】:

标签: android android-asynctask


【解决方案1】:

来自文档,

Android 6.0 版本删除了对 Apache HTTP 客户端的支持。如果您的应用正在使用此客户端并针对 Android 2.3(API 级别 9)或更高版本,请改用 HttpURLConnection 类。此 API 更高效,因为它通过透明压缩和响应缓存减少了网络使用,并将功耗降至最低。要继续使用 Apache HTTP API,您必须首先在 build.gradle 文件中声明以下编译时依赖项:

android {
    useLibrary 'org.apache.http.legacy'
}

【讨论】:

    【解决方案2】:

    HttpGet、HttpClient、HttpResponse 和 HttpEntity 已弃用。
    现在你必须使用HttpUrlConnection

    URL url = new URL("http://www.android.com/");
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
       try {
         InputStream in = new BufferedInputStream(urlConnection.getInputStream());
         readStream(in);
        finally {
         urlConnection.disconnect();
       }
     }
    

    【讨论】:

    • 谢谢分配! ,我想这确实是个菜鸟,但它无法为我解析 readStream。
    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 2011-10-12
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多