search

Home  >  Q&A  >  body text

android使用okhttp总是报错,代码在java工程中运行没有问题

报错如下:

代码如下:

public class httpRequest {
OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
    Request request = new Request.Builder().url(url).build();
    Response response = client.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().string();
    } else {
        throw new IOException("Unexpected code " + response);
    }
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.tt);
    httpRequest request = new httpRequest();
    try {
        String response = request.run("https://www.baidu.com");
        tv.setText(response);
        System.out.println(response);
        Log.d("res", response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("error", "error!!!!!");
        e.printStackTrace();
    }
}

用okhttp3也是这样,提示找不到okhttp.okHttpClient
PHPzPHPz2771 days ago578

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 17:31:45

    Is there any package that imports okHttp in the android project? The error message indicates that the OkHttpClient class was not found

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:31:45

    Did you import the jar package or add it directly in compile?

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:31:45

    When using okhttp, you need to add another package dependency

    compile 'com.squareup.okio:okio:1.5.0'

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:31:45

    1. The dex in DexPathList does not contain okhttpclient, which means that you did not enter the dependency package of okhttp into the apk when packaging.
    2. It is recommended to switch from eclipse to android studio

    reply
    0
  • Cancelreply