Question

What I am trying to do::

I am trying to learn the usage of Okhttp for making networking calls in android


What I have done::

  • I have read their documentation here
  • I have downloaded and added the JAR in the project
  • I am using their sample code from 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());

        }

    }

Error i am facing::

In the line Response response = client.execute(request); I am getting error as :

client cannot resolved to a variable

How to resolve this !


{Update}

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

    }

}

Now I am facing error as :

in the line Response response = client.execute(request); as The method execute(Request) is undefined for the type OkHttpClient

Was it helpful?

Solution

The method execute(Request) is undefined for the type OkHttpClient

You are getting this exception because there is no such method ie.execute(Request) for OkHttpClient. Rather it is invoked on Call object which is obtained using OkHttpClient object as follows:

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

I think you should be using

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

instead of Response response = client.execute(request);

OkHttp docs

OkHttp Blog

OTHER TIPS

I think you should use the new 2.0 RC of okHttp.

To make a POST petition, the best is :

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

}
@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;        
}

use like this!

you are missing this line

OkHttpClient client = new OkHttpClient();

Try adding internet permissions in your manifest file?

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

You need declare client variable like this:

OkHttpClient client = new OkHttpClient();

Also you should use your request using some thread or AsyncTask!

You can read something else about AsyncTask on Android in this link: http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

You only need to replace: This:

Response response = client.execute(request);

By:

Response response = client.newCall(request).execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top