【问题标题】:okhttp not displaying message in logcatokhttp 未在 logcat 中显示消息
【发布时间】:2015-09-25 08:25:05
【问题描述】:

我在下面的代码中尝试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());
}

这是日志猫

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

【问题讨论】:

  • 你已经把它放在 asynctask 的 doInBackground 里面了吗?
  • 我正在使用okhttp .. 是否包括添加doinbackground?我们在 asynchtask 中使用 doinbackground 吗?在示例中他们没有提到@BNK
  • 你必须这样做:),我检查了你的代码,它在 asynctask 中工作
  • @BNK 我正在学习 OkHttp,因为在最新的 SDK 中我无法使用 asynctask .. 请检查链接 stackoverflow.com/questions/32380439/…
  • 你的项目如何使用okhttp,我的项目在libs文件夹中包含okhttp-2.5.0.jarokio-1.6.0.jar

标签: android okhttp


【解决方案1】:

正如我所说,请尝试以下代码:

    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();

【讨论】:

  • 我必须为此创建 2 个活动,我可以将它们放在一个活动中吗?
  • 为什么要进行 2 项活动?我的示例项目只有一个 MainActivity :)
  • 好的,谢谢。但是有些东西我没明白,如果你能帮助我,请..我使用 Okhttp 来避免使用 asynctask,(正如我在链接中解释的那样)为什么在上面的例子中我们使用 asynctask ?
  • OkHttp 的异步方法请阅读stackoverflow.com/questions/28135338/… :)
  • 啊是的,它比 volley 更好,我从 volley 开始,但它让我头疼,所以我从 picasso 开始。很简单。
猜你喜欢
  • 2017-08-03
  • 1970-01-01
  • 2016-03-13
  • 2011-11-28
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
相关资源
最近更新 更多