Java DefaultHttpClient已弃用Android Studio 2.2.2
在遵循了一些关于如何使用Apache获取数据并将数据发送到本地服务器上的数据库的指南之后,出现了一个错误,指出该代码已被弃用。在查找了有关弃用的指南后,我发现大多数指南都过时了。 以下是我的清单、解析器和Gradle文件模块的代码(按顺序):Java DefaultHttpClient已弃用Android Studio 2.2.2,java,android,apache,http,Java,Android,Apache,Http,在遵循了一些关于如何使用Apache获取数据并将数据发送到本地服务器上的数据库的指南之后,出现了一个错误,指出该代码已被弃用。在查找了有关弃用的指南后,我发现大多数指南都过时了。 以下是我的清单、解析器和Gradle文件模块的代码(按顺序): 我需要采取什么步骤来解决这个问题? 此外,在UrlEncodedFormEntity中,无法接收ContentValues对象,我如何解决该问题?[Update] 我强烈建议使用而不是org.apache.http。它更快(编码时间),维护/编码更简单
我需要采取什么步骤来解决这个问题? 此外,在UrlEncodedFormEntity中,无法接收ContentValues对象,我如何解决该问题?[Update] 我强烈建议使用而不是
org.apache.http
。它更快(编码时间),维护/编码更简单
获取请求
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
发布请求
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();
}
到目前为止还没有找到比这更好的
只需添加以下依赖项
compile 'com.squareup.okhttp3:okhttp:3.4.2'
完整的代码
import android.content.ContentValues;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class JSONParser {
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public static JSONObject makeHttpRequest(String url, String method, ContentValues params) {
// Making HTTP request
try {
final OkHttpClient client = new OkHttpClient();
Request request;
// check for request method
if (method.equals("POST")) {
// request method is POST
final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
final RequestBody body = RequestBody.create(JSON, params.toString());
request = new Request.Builder()
.url(url)
.post(body)
.build();
} else if (method.equals("GET")) {
// request method is GET
request = new Request.Builder()
.url(url)
.build();
}
final Response response = client.newCall(request).execute();
json = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e ){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
代码中的另一个问题是这种比较:
method==“POST”
。使用equals
方法比较字符串
值,而不是=
。谢谢,我现在已经更改了它,我是将代码添加到解析器中还是用您编写的内容替换所有文件?我从来没有使用过这些请求,因为我以前没有学习过它们,也没有使用过它们。我现在用我的phoe发表评论,我会在我拿到我的电脑后立即给你完整的等价物以及你需要做的事情。@Swane,我已经更新了我的答案。请检查它,让我现在如果它的工作。谢谢你的回答,代码的工作,但有一些更多的错误在应用程序的过程中,张贴在这里或打开一个新的问题吗?很高兴它有帮助。我认为你应该提出一个新问题。
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();
}
compile 'com.squareup.okhttp3:okhttp:3.4.2'
import android.content.ContentValues;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class JSONParser {
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public static JSONObject makeHttpRequest(String url, String method, ContentValues params) {
// Making HTTP request
try {
final OkHttpClient client = new OkHttpClient();
Request request;
// check for request method
if (method.equals("POST")) {
// request method is POST
final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
final RequestBody body = RequestBody.create(JSON, params.toString());
request = new Request.Builder()
.url(url)
.post(body)
.build();
} else if (method.equals("GET")) {
// request method is GET
request = new Request.Builder()
.url(url)
.build();
}
final Response response = client.newCall(request).execute();
json = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e ){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}