HttpGet、HttpClient、HttpResponse、HttpEntity、EntityUtils 已弃用

HttpGet, HttpClient, HttpResponse, HttpEntity, EntityUtils deprectated

我正在使用 android studio API 23 我有这些警告

这是我的代码

@Override
    protected Boolean doInBackground(String... urls) {
        String strNama[], strDeskripsi[], strFoto[], strMarker[], strLng[], strLat[];

        try {
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);
            int status = response.getStatusLine().getStatusCode();
            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
                JSONObject jsono = new JSONObject(data);
                JSONArray konten = jsono.getJSONArray("konten");
                strNama = new String[konten.length()];
                strDeskripsi = new String[konten.length()];
                strFoto = new String[konten.length()];
                strMarker = new String[konten.length()];
                strLat = new String[konten.length()];
                strLng = new String[konten.length()];
                for (int i = 0; i < konten.length(); i++) {
                    JSONObject object = konten.getJSONObject(i);
                    strNama[i] = object.getString("nama");
                    strDeskripsi[i] = object.getString("deskripsi");
                    strFoto[i] = object.getString("foto");
                    strMarker[i] = object.getString("marker");
                    strLat[i] = object.getString("lat");
                    strLng[i] = object.getString("lng");
                    Actors actor = new Actors();
                    actor.setName(strNama[i]);
                    actor.setDescription(strDeskripsi[i]);
                    actor.setImage(strFoto[i]);
                    actor.setMarker(strMarker[i]);
                    actor.setLat(strLat[i]);
                    actor.setLng(strLng[i]);
                    actorsList.add(actor);
                }
                return true;
            }

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

这是模块

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.krb.navigasi.petakebunrayabogor"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

它在 android 5.0 中运行良好,但我该如何解决这些警告?我希望有人可以帮助我修复上面的代码。任何帮助将不胜感激。

提前致谢。

由于已经回答提到的 classes 已被弃用,Android 文档建议您使用 HttpURLConnection,您可以在其中自行处理网络调用,确保您编写他们离开了主线程。

下面给出了一个示例,您可以如何 POST 使用 HttpURLConnection

的实体
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST"); // hear you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
            httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json` 
            httpURLConnection.connect();

并且当您post将一些数据发送到 HttpURLConnection 的实例时,您可以这样做...

            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("para_1", "arg_1");
            jsonObject.addProperty("para_2", "arg_2");

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();

由于此 class 与 android 框架捆绑在一起,因此无需添加任何库,但我建议您使用 OkHTTP

之类的东西

这将处理其他线程的网络调用,并给出示例显示如何 post

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

此示例在主线程上调用网络调用,您宁愿像这样进行排队

    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

        }
    });

只要初始化这个,然后同步

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.cli‌​ent:4.1.2'