gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:40:52 26 4
gpt4 key购买 nike

我正在使用 android studio API 23 并且我有这些警告 deprecated warning

这是我的代码

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

提前致谢。

最佳答案

由于已经回答提到的类已被弃用,Android 文档建议您使用 HttpURLConnection在你可以自己处理网络调用的地方确保你将它们从主线程中删除。

下面给出的示例是如何使用 HttpURLConnectionPOST 实体

            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 框架 bundle 在一起,因此无需添加任何库,但我建议您使用类似 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 {

}
});

关于java - HttpGet、HttpClient、HttpResponse、HttpEntity、EntityUtils 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com