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>