16

我正在尝试做的事情::

我正在尝试学习Okhttp 在 android 中进行网络调用的用法


我做了什么::


我的代码::

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.internal.http.Request;
import com.squareup.okhttp.internal.http.Response;
    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

             Request request = new Request.Builder()
                .url("https://raw.github.com/square/okhttp/master/README.md")
                .build();

            Response response = client.execute(request);

            Log.d("ANSWER", response.body().string());

        }

    }

我面临的错误::

在这一行中Response response = client.execute(request); ,我收到错误:

客户端无法解析为变量

如何解决这个问题!


{更新}

public class MainActivity extends Activity {

    OkHttpClient client = new OkHttpClient();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Request request = new Request.Builder()
            .url("https://raw.github.com/square/okhttp/master/README.md")
            .build();

        Response response = client.execute(request);

        Log.d("ANSWER", response.body().string());

    }

}

现在我面临错误:

在该行Response response = client.execute(request);The method execute(Request) is undefined for the type OkHttpClient

4

8 回答 8

13

未定义 OkHttpClient 类型的 execute(Request) 方法

你得到这个异常是因为没有这样的方法,即。execute(Request) 为OkHttpClient。相反,它是在Call使用 object 获得的 object 上调用的OkHttpClient,如下所示:

  Call call = client.newCall(request);
  Response response = call.execute();

我认为你应该使用

Response response = client.newCall(request).execute();

代替Response response = client.execute(request);

OkHttp 文档

OkHttp 博客

于 2014-07-03T09:57:46.890 回答
4

我认为你应该使用 okHttp 的新 2.0 RC。

要进行 POST 请愿,最好的方法是:

String post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}
于 2014-06-19T07:13:48.787 回答
3
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new AsyncTask<String, Integer, String> (){

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                client = new OkHttpClient();
                s = post("https://raw.github.com/square/okhttp/master/README.md","");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.e("ANSWER", "" + s);

        }
    }.execute();

}
于 2014-07-03T09:52:46.893 回答
2
@Override
protected String doInBackground(String... ulr) {
    Response response = null;
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
    .url(ulr[0])
    .build();

    try {
        response = client.newCall(request).execute();
         return response.body().string();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;        
}

像这样使用!

于 2015-07-30T17:30:35.103 回答
1

你错过了这一行

OkHttpClient client = new OkHttpClient();

于 2014-04-21T16:50:09.313 回答
1

尝试在清单文件中添加 Internet 权限?

<uses-permission android:name="android.permission.INTERNET" />
于 2015-09-08T20:55:19.287 回答
0

您需要像这样声明客户端变量:

OkHttpClient client = new OkHttpClient();

此外,您应该使用一些线程或 AsyncTask 来使用您的请求!

您可以在此链接中阅读有关 Android 上 AsyncTask 的其他内容:http ://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

于 2014-04-21T16:55:56.417 回答
0

你只需要替换: 这个:

Response response = client.execute(request);

经过:

Response response = client.newCall(request).execute();
于 2015-07-27T08:42:11.323 回答