OkHttp在android中用于发出网络请求

时间:2014-04-21 16:43:28

标签: java android okhttp

我想做什么 ::

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


我做了什么 ::

  • 我已阅读他们的文档here
  • 我已在项目中下载并添加了JAR
  • 我正在使用here
  • 中的示例代码

mycode的 ::

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

8 个答案:

答案 0 :(得分:13)

  

对于类型OkHttpClient

,方法execute(Request)未定义

您收到此异常是因为execute(Request没有此类方法,即OkHttpClient}。而是在使用Call对象获得的OkHttpClient对象上调用它,如下所示:

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

我认为你应该使用

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

而不是Response response = client.execute(request);

OkHttp docs

OkHttp Blog

答案 1 :(得分: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();
}

答案 2 :(得分: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();

}

答案 3 :(得分: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;        
}

像这样使用!

答案 4 :(得分:1)

你错过了这一行

OkHttpClient client = new OkHttpClient();

答案 5 :(得分:1)

尝试在清单文件中添加互联网权限?

<uses-permission android:name="android.permission.INTERNET" />

答案 6 :(得分:0)

您需要声明客户端变量,如下所示:

OkHttpClient client = new OkHttpClient();

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

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

答案 7 :(得分:0)

您只需要替换: :此:

Response response = client.execute(request);

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