1

I'm trying to fetch json data from a server and currently displaying that data as it is without parsing, in a textview. The problem is that the data received is always null but when i visit the json url it displays json data. When I'm using http://api.androidhive.info/contacts/ as json url it is working. But when I'm using https://jsonblob.com/api/jsonBlob/56aa6129e4b01190df4c0b87 as json url it isn't . I can't find the reason behind it .It is weird. Any thing that I'm missing ?

Here is my MainActivity

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    ProgressDialog progressDialog;
    final int CONN_TIME = 1000 * 15;
    final String SERVER_ADDRESS = "http://api.androidhive.info/contacts/";

    TextView txtView;
    String ans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtView = (TextView)findViewById(R.id.screen);

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setTitle("Processing");
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
        SyncUserDataAsyncTask s = new SyncUserDataAsyncTask();
        s.execute();
    }

    public class SyncUserDataAsyncTask extends AsyncTask<Void, Void,    JSONArray> {

    // User user;

        SyncUserDataAsyncTask() {

        }

        @Override
        protected JSONArray doInBackground(Void... params) {

            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                 CONN_TIME);
            HttpConnectionParams.setSoTimeout(httpRequestParams, CONN_TIME);

            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(SERVER_ADDRESS);

            JSONArray returnedArray = null;
            try {

                HttpResponse httpResponse = client.execute(post);

                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                ans = result;
                JSONArray jArray = new JSONArray(result);
                if (jArray.length() == 0) {
                    returnedArray = null;
                } else {
                    returnedArray = jArray;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return returnedArray;
        }

        @Override
        protected void onPostExecute(JSONArray returnedArray) {
            progressDialog.dismiss();
            // callBack.done(returnedArray);
            //super.onPostExecute(returnedArray);
            txtView.setText(ans);
        }
    }

}

here is mainactivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.co.MainActivity" >

    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#E55A25" />

    <TextView
        android:id="@+id/screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:background="#acacac"
        android:textColor="#ffffff"
        android:text="hello,world" />
 </RelativeLayout>
Pratik Saxena
  • 191
  • 2
  • 10

2 Answers2

0

Your problem is that the 1st link works and the 2nd one doesn't is because the 1st link is an http link, whereas the 2nd link is an https link. The code is going in the catch block and the exception is

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

To solve this issue, take a look at the answers here.

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75
0

As a workaround I recommend you to use OkHttp library for networking. Your async task would be something like this.

public class SyncUserDataAsyncTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(SERVER_ADDRESS)
                    .build();

            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String response) {
        progressDialog.dismiss();
        txtView.setText(response);
    }
}

Add in your build.gradle

compile "com.squareup.okhttp3:okhttp:3.0.1"

I tested both urls and worked fine

Omar Mainegra
  • 4,006
  • 22
  • 30