okhttp 未在 logcat 中显示消息
okhttp not displaying message in logcat
我正在尝试以下代码中的 OkHttp
个示例我期待一些打印消息,但是我没有收到任何东西,你能告诉我为什么吗?
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
这是logcat
Function: selinux_android_load_priority [0], There is no sepolicy file.
selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_SM-N900_4.4.2_0040
09-25 11:19:24.695 28481-28481/? I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
09-25 11:19:24.695 28481-28481/? E/dalvikvm﹕ >>>>> Normal User
09-25 11:19:24.695 28481-28481/? E/dalvikvm﹕ >>>>> com.justapps.ak [ userId:0 | appId:10203 ]
09-25 11:19:24.700 28481-28481/? D/dalvikvm﹕ Late-enabling CheckJNI
W/ApplicationPackageManager﹕ getCSCPackageItemText()
I/PersonaManager﹕ getPersonaService() name persona_policy
PLATFORM VERSION : JB-MR-2
D/mali_winsys﹕ new_window_surface returns 0x3000
D/OpenGLRenderer﹕ Enabling debug mode 0
正如我评论的那样,请尝试以下代码:
private class VoidRequest extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
MyOkHttpRequest request = new MyOkHttpRequest();
try {
request.run();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public class MyOkHttpRequest {
private OkHttpClient client = new OkHttpClient();
public void run() throws IOException {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
}
然后在onCreate()
中调用new VoidRequest().execute();
我正在尝试以下代码中的 OkHttp
个示例我期待一些打印消息,但是我没有收到任何东西,你能告诉我为什么吗?
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
这是logcat
Function: selinux_android_load_priority [0], There is no sepolicy file.
selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_SM-N900_4.4.2_0040
09-25 11:19:24.695 28481-28481/? I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
09-25 11:19:24.695 28481-28481/? E/dalvikvm﹕ >>>>> Normal User
09-25 11:19:24.695 28481-28481/? E/dalvikvm﹕ >>>>> com.justapps.ak [ userId:0 | appId:10203 ]
09-25 11:19:24.700 28481-28481/? D/dalvikvm﹕ Late-enabling CheckJNI
W/ApplicationPackageManager﹕ getCSCPackageItemText()
I/PersonaManager﹕ getPersonaService() name persona_policy
PLATFORM VERSION : JB-MR-2
D/mali_winsys﹕ new_window_surface returns 0x3000
D/OpenGLRenderer﹕ Enabling debug mode 0
正如我评论的那样,请尝试以下代码:
private class VoidRequest extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
MyOkHttpRequest request = new MyOkHttpRequest();
try {
request.run();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public class MyOkHttpRequest {
private OkHttpClient client = new OkHttpClient();
public void run() throws IOException {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
}
然后在onCreate()
中调用new VoidRequest().execute();