OkHttp Client returns 空值
OkHttp Client returns a null value
我正在尝试将 okhttpclient 与 okhttp3 一起使用,但它return 是一个空值。
这是我的代码:
OkHttpClient client = new OkHttpClient();
HttpUrl.Builder urlBuilder = HttpUrl.parse("http://www.test.com/get_data.php").newBuilder();
urlBuilder.addEncodedQueryParameter("GroupName", groupName);
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String responseData = response.body().string();
JSONObject jsonObj = new JSONObject(responseData);
jArray = jsonObj.getJSONArray("result");
} catch (JSONException e) {
}
}
});
然后响应数据 returns {"result":[]}
我尝试使用与上面相同的 url,但使用 httpClient 的方法不同,它 return 是我想要的值,这里是 httpclient 代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.test.com/get_data.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
StringBuilder sb = new StringBuilder();
String result = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()), 65728);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
if(sb.toString().trim().length() > 0) {
result = sb.toString();
return result;
}
}
请帮助我,有 okhttp 经验的人,为什么我需要 okhttp?
因为 sdk 23 中已经弃用了 httpClient,谢谢。
您没有在客户端对象上执行。尝试添加以下代码片段:
Response response = client.newCall(request).execute();
return response.body().string();
为了参考例子,请看下面link:http://www.vogella.com/tutorials/JavaLibrary-OkHttp/article.html
我正在尝试将 okhttpclient 与 okhttp3 一起使用,但它return 是一个空值。
这是我的代码:
OkHttpClient client = new OkHttpClient();
HttpUrl.Builder urlBuilder = HttpUrl.parse("http://www.test.com/get_data.php").newBuilder();
urlBuilder.addEncodedQueryParameter("GroupName", groupName);
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String responseData = response.body().string();
JSONObject jsonObj = new JSONObject(responseData);
jArray = jsonObj.getJSONArray("result");
} catch (JSONException e) {
}
}
});
然后响应数据 returns {"result":[]}
我尝试使用与上面相同的 url,但使用 httpClient 的方法不同,它 return 是我想要的值,这里是 httpclient 代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.test.com/get_data.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
StringBuilder sb = new StringBuilder();
String result = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()), 65728);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
if(sb.toString().trim().length() > 0) {
result = sb.toString();
return result;
}
}
请帮助我,有 okhttp 经验的人,为什么我需要 okhttp? 因为 sdk 23 中已经弃用了 httpClient,谢谢。
您没有在客户端对象上执行。尝试添加以下代码片段:
Response response = client.newCall(request).execute();
return response.body().string();
为了参考例子,请看下面link:http://www.vogella.com/tutorials/JavaLibrary-OkHttp/article.html