【问题标题】:HttpGet, HttpClient, HttpResponse, HttpEntity, EntityUtils deprectated已弃用 HttpGet、HttpClient、HttpResponse、HttpEntity、EntityUtils
【发布时间】:2016-03-23 04:47:26
【问题描述】:

我正在使用 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 中运行良好,但我该如何解决这些警告?我希望有人可以帮助我修复上面的代码。任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • Apache Http 模块已在最新版本中被移除。所以你必须选择另一个选项,比如 OkHttp,Volley 之类的。
  • @Michael Julyus Christopher M.:减少 compileSdkVersion、buildToolsVersion 和 targetSdkVersion 的值,因为它们在 API 23 中已被弃用,所以可以使用。

标签: java android apache-httpclient-4.x apache-httpcomponents


【解决方案1】:

由于已经回答过提到的类已被弃用,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();

当您将一些数据发布到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();

由于这个类与android框架捆绑在一起,不需要添加任何库,但我建议你使用OkHTTP之类的东西,

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

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 {

        }
    });

【讨论】:

  • 你能帮我修复我上面发布的代码吗?抱歉,我是 android 新手 :(
  • 您可以使用答案中的代码,两个 sn-p 都可以进行微小的更改,如果您仍然遇到任何问题,请将您从中获得响应的 URL 传递给我,我可能会告诉您什么去做
【解决方案2】:

只需初始化它,然后同步

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

【讨论】:

  • 你在android 6.0上运行过吗?它在 android 6.0 中可以正常工作吗?
  • 我已经尝试过您编辑的代码,但它显示了已弃用的警告。但是,如果我像这样使用您以前的答案compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2',则已弃用的警告将消失。那么,哪一个是真的?您之前的答案还是修改后的答案?
  • 是的,但我还没有在 android 6.0 中尝试过。我已经在 android 5.0 中尝试过,效果很好。但是我仍然不确定android 6.0是否成功。有没有直接在android 6.0的设备上试过(不是模拟器)?
  • 抱歉,我无法尝试,因为我的设备是 android 5.0,我无法使用模拟器尝试,因为如果我运行模拟器,我的笔记本电脑会滞后。你在 android 6.0 中尝试过吗?还是谢谢。
  • 我说是的......它在 6.0 中工作......你需要自己尝试......如果有帮助,请接受答案......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多