【发布时间】:2018-07-24 19:57:02
【问题描述】:
我尝试从服务器获取 json,我在服务器上使用 https,每个 http 请求都会转到 https 版本。
我得到了数据并且我发送到的数据可以工作,但是最多需要 45 秒才能得到回复。使用 android 的内置 http 处理程序,相同的代码更快。
如何加快请求速度?
try {
OkHttpClient client = new OkHttpClient();
FormBody.Builder formBuilder = new FormBody.Builder().add("key", "2285");
//formBuilder.add("phone", "000000");
RequestBody formBody = formBuilder.build();
Log.v("JsonRespons", reqUrl);
Request request = new Request.Builder()
.url(reqUrl)
.post(formBody)
.build();
okhttp3.Response response = null;
response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException(response.message() + " " + response.toString());
} else {
output = response.body().string();
}
if(output != null) {
Log.v("JsonRespons", output.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
你在哪个线程上执行?如果您对线程知之甚少,只需按照以下答案中的方式选择异步即可。 stackoverflow.com/questions/34967505/…
-
@toshkinl - 我已经构建了一个从 url 获取 json 的类,这部分代码来自该类