搜尋

首頁  >  問答  >  主體

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
PHPzPHPz2887 天前628

全部回覆(4)我來回復

  • 黄舟

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

    android專案裡面有沒有導入okHttp的包啊?報錯訊息指明了OkHttpClient這個類別沒有找到啊

    回覆
    0
  • 天蓬老师

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

    你是導入jar包還是直接compile裡加的?

    回覆
    0
  • 天蓬老师

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

    使用okhttp的時候還要再增加一個套件的依賴

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

    回覆
    0
  • 伊谢尔伦

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

    1、DexPathList中的dex中未包含okhttpclient,說明你打包的時候,沒有把okhttp的依賴包打進apk中
    2、建議從eclipse轉到android studio

    回覆
    0
  • 取消回覆