AppGeek
AppGeek

Reputation: 137

Problems in getting json data from site

So i am using the okHttp client to get some weather data from a site. Till now I wrote the code just to get the json data and display it but im not getting the data.By the way I have checked the url twice and it works. Heres the code:

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        OkHttpClient client = new OkHttpClient();
        final TextView tv = (TextView)rootView.findViewById(R.id.display_json_data);
        Request request = new Request.Builder().url("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7&APPID=c82bfbedf3241dd88e356bffea122761").build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String jsonData = response.body().string();
                tv.setText(jsonData);
            }
        });

        return rootView;

    }
}

I have added the permission required in the manifest. I get no output when I run it on my device. Please help.

Upvotes: 2

Views: 119

Answers (2)

Imanali Mamadiev
Imanali Mamadiev

Reputation: 2654

Check your Permissions in AndroidManifest:

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

You can use UI Thread "runOnUiThread":

OkHttpClient client = new OkHttpClient();
final TextView tv = (TextView) findViewById(R.id.display_json_data);
Request request = new Request.Builder().url("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7&APPID=c82bfbedf3241dd88e356bffea122761").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {

     @Override
     public void onFailure(Request request, IOException e) {

     }

     @Override
     public void onResponse(Response response) throws IOException {
        final String jsonData = response.body().string();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
               tv.setText(jsonData);
            }
        });
     }
});

Upvotes: 1

Md Rahman
Md Rahman

Reputation: 1820

You can try this.

  Response response = client.newCall(request).execute();
  return response.body().string();

Permissions :

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

Reference Link : http://square.github.io/okhttp/

Upvotes: 0

Related Questions