1

I am trying OkHttp examples in this below code I was expecting some printing message, however I am not recieving anything , can you tell me why?

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
    Request request = new Request.Builder()
            .url("http://publicobject.com/helloworld.txt")
            .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
        System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }

    System.out.println(response.body().string());
}

this is the logcat

Function: selinux_android_load_priority [0], There is no sepolicy file.
selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_SM-N900_4.4.2_0040
09-25 11:19:24.695  28481-28481/? I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
09-25 11:19:24.695  28481-28481/? E/dalvikvm﹕ >>>>> Normal User
09-25 11:19:24.695  28481-28481/? E/dalvikvm﹕ >>>>> com.justapps.ak [ userId:0 | appId:10203 ]
09-25 11:19:24.700  28481-28481/? D/dalvikvm﹕ Late-enabling CheckJNI
W/ApplicationPackageManager﹕ getCSCPackageItemText()
I/PersonaManager﹕ getPersonaService() name persona_policy
PLATFORM VERSION : JB-MR-2
D/mali_winsys﹕ new_window_surface returns 0x3000
D/OpenGLRenderer﹕ Enabling debug mode 0
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • Have you put it inside `doInBackground` of asynctask yet? – BNK Sep 25 '15 at 08:34
  • I am using `okhttp` .. does that include to add `doinbackground`?Do we use doinbackground in asynchtask ? in the examples they didnt mention that @BNK – Moudiz Sep 25 '15 at 08:39
  • You have to do so :), I check your code, it works in the asynctask – BNK Sep 25 '15 at 08:40
  • @BNK I am learning OkHttp because in the latest SDK I cant work with asynctask.. check please the link http://stackoverflow.com/questions/32380439/namevaluepair-error-namevaluepair-cannot-be-resolved-to-a-type/32380569?noredirect=1#comment53247496_32380569 – Moudiz Sep 25 '15 at 08:42
  • How does your project use okhttp, my project includes `okhttp-2.5.0.jar` and `okio-1.6.0.jar` in `libs` folder – BNK Sep 25 '15 at 08:47
  • @BNK sorry I didnt understand your question – Moudiz Sep 25 '15 at 08:52
  • I mean that how does your project include Okhttp library. Have you downloaded the jar file from http://square.github.io/okhttp/? – BNK Sep 25 '15 at 08:53
  • its in my Gradle file , dependencies { compile 'com.squareup.okhttp:okhttp:2.4.0' compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' } – Moudiz Sep 25 '15 at 08:56
  • I try `compile 'com.squareup.okhttp:okhttp:2.5.0'` instead of Jar files, must have asynctask also – BNK Sep 25 '15 at 09:02
  • Ah, you can read more here http://stackoverflow.com/questions/28135338/okhttp-library-networkonmainthreadexception-on-simple-post – BNK Sep 25 '15 at 09:06

1 Answers1

1

As I commented, please try the following code:

    private class VoidRequest extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            MyOkHttpRequest request = new MyOkHttpRequest();
            try {
                request.run();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public class MyOkHttpRequest {
        private OkHttpClient client = new OkHttpClient();
        public void run() throws IOException {
            Request request = new Request.Builder()
                    .url("http://publicobject.com/helloworld.txt")
                    .build();
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            Headers responseHeaders = response.headers();
            for (int i = 0; i < responseHeaders.size(); i++) {
                System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
            }
            System.out.println(response.body().string());
        }
    }

Then in onCreate(), call new VoidRequest().execute();

BNK
  • 23,994
  • 8
  • 77
  • 87
  • I had to create 2 activities for that ,can I have them in one activity ? – Moudiz Sep 25 '15 at 09:12
  • Why 2 activities? My sample project has only one MainActivity :) – BNK Sep 25 '15 at 09:14
  • ok thank it worked . but there is something I didnt get it , if you can help me please .. I am using Okhttp to avoid using asynctask,(as I explained in the link) why then in above example we used asynctask ? – Moudiz Sep 25 '15 at 09:24
  • Please read http://stackoverflow.com/questions/28135338/okhttp-library-networkonmainthreadexception-on-simple-post for OkHttp's async method :) – BNK Sep 25 '15 at 09:25
  • Yes I am reading , the piece of code where should be added ? call post string(url ... ? – Moudiz Sep 25 '15 at 09:33
  • Tomorrow I will try OkHttp async more. Actually, I start learning OkHttp like you :-) – BNK Sep 25 '15 at 09:35
  • oh thats good , can you please when you learn it show me your way how to upload an image and receive it from server ? because I am learning to do that , and I want to compare your way with my way. if you will do that tell me – Moudiz Sep 25 '15 at 09:43
  • Yes, I see, however, why don't you try Retrofit or Volley for that? :) – BNK Sep 25 '15 at 09:47
  • I am using picasso , with that I need something like OkHttp right? – Moudiz Sep 25 '15 at 09:58
  • I haven't used Piccaso, however I see many people recommend it :-) – BNK Sep 25 '15 at 10:00
  • 1
    ah yes its better than volley, I started with volley but it caused me headech so I started with picasso . its simple. – Moudiz Sep 25 '15 at 10:04