0

I am using okHttp .
My URL is okay.
But when i run that app on a emulator or a real device it never shows any error . But it's not logging the weather data.

Mainactivity.java

   package com.buckydroid.droidweather;

import android.app.DownloadManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    public static String responsedata;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text = (TextView)findViewById(R.id.text) ;

        new AsyncTask<Void, Void, String>()  {
            @Override
            protected String doInBackground(Void... params) {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url("https://api.forecast.io/forecast/11f696255d54c7cea3d097a6338bbaed/37.8267,-122.423").build();
                try {

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

                    responsedata = response.body().toString();
                    Log.d(TAG,responsedata);
                    return  responsedata;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return responsedata;
            }
        }.execute();



    }
}

LOG

 08-04 17:27:31.847 18694-18694/com.buckydroid.droidweather I/art: Not late-enabling -Xcheck:jni (already on)
08-04 17:27:31.936 18694-18694/com.buckydroid.droidweather W/System: ClassLoader referenced unknown path: /data/app/com.buckydroid.droidweather-2/lib/x86
08-04 17:27:52.479 18694-18694/com.buckydroid.droidweather W/System: ClassLoader referenced unknown path: /data/app/com.buckydroid.droidweather-2/lib/x86
08-04 17:27:52.562 18694-18694/com.buckydroid.droidweather W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-04 17:27:52.603 18694-19012/com.buckydroid.droidweather D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                             [ 08-04 17:27:52.613 18694:18694 D/         ]
                                                                             HostConnection::get() New Host Connection established 0xaa99da20, tid 18694


                                                                             [ 08-04 17:27:52.668 18694:19012 D/         ]
                                                                             HostConnection::get() New Host Connection established 0xae407270, tid 19012
08-04 17:27:52.671 18694-19012/com.buckydroid.droidweather I/OpenGLRenderer: Initialized EGL, version 1.4
08-04 17:27:54.061 18694-18694/com.buckydroid.droidweather W/art: Verification of boolean android.support.v7.widget.ActionMenuPresenter.isOverflowReserved() took 180.240ms
08-04 17:27:56.127 18694-19011/com.buckydroid.droidweather D/MainActivity: okhttp3.internal.http.RealResponseBody@fe12d95

And i added the internet permission . So no problem in that

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Configure logging on your `OkHttpClient` and see what exactly happens when you're making the request. – Egor Aug 04 '16 at 10:43
  • You need to execute() the task your created https://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...) – Serg Aug 04 '16 at 10:47
  • i ran it again it's showing getslotfrombuffer locked –  Aug 04 '16 at 10:55
  • can u tell me how to fix it –  Aug 04 '16 at 10:55
  • oops it's showing fatal exception @Serg –  Aug 04 '16 at 11:04
  • @BuckyĎroid Ok, it started to run :) You created `AsyncTask` but returning `null` from `doInBackground`. Edit your question to show latest version of code and the error. – Serg Aug 04 '16 at 11:16
  • There is another GetSlotFromBufferLocked at the end @Serg –  Aug 04 '16 at 11:21
  • By the way, you don't need an AsyncTask to use OkHttp. It supposts Asynchronous requests on its own – OneCricketeer Aug 04 '16 at 12:09
  • but we can do it using asynctask @cricket_007 –  Aug 04 '16 at 12:10
  • But you don't need to, and that's likely a cause of your problem / confusion / error. https://github.com/square/okhttp/wiki/Recipes#asynchronous-get – OneCricketeer Aug 04 '16 at 12:15
  • https://www.youtube.com/watch?v=x3wBEYGd_3Y i was following that tutorial . It worked for him . And i also tried it using asynchronus process. But it was showinggetslotfrombuffer locked error @cricket_007 –  Aug 04 '16 at 12:33
  • That isn't an error message for your app. You can safely ignore it - I get the exact same thing sometimes. And your log has no errors - you **are** printing the response object – OneCricketeer Aug 04 '16 at 12:35
  • How can i see the weather data then @cricket_007 –  Aug 04 '16 at 12:39
  • o shit it worked thank you soooo much @cricket_007 I just replaced toString with string and weather data is in the log –  Aug 04 '16 at 12:46
  • If you watch that part of the video again, it also uses that method – OneCricketeer Aug 04 '16 at 12:49
  • I didn't give that part any importance . Cause i knew i have to convert it to string only. @cricket_007 –  Aug 04 '16 at 12:59

1 Answers1

0

This fragment:

            Log.d(TAG,responsedata);
            responsedata = response.body().string();

It should be in a reverse order.
And next, you need to return String from doInBackground according to AsyncTask<Void, Void, String>
And one more, common practice is to extend AsyncTask and use this your own class.

Serg
  • 22,285
  • 5
  • 21
  • 48
  • It's not showing any weather data but good news is not showing any error Only a log D/MainActivity: okhttp3.internal.http.RealResponseBody@501feaa –  Aug 04 '16 at 11:39
  • updated the queestion –  Aug 04 '16 at 12:00
  • I just replaced toString with string and weather data is in the log –  Aug 04 '16 at 12:47
  • toSting() most probably returns object string represantation, not the value you need. Ahh, I see you already done it. – Serg Aug 04 '16 at 12:47
  • :) Now everything fine. –  Aug 04 '16 at 12:49