I have a JSONObject
receiving from an API call within a AsyncTask
class. I want to pass that JSON object to my MainActivity
class to show the JSONObject
data in the GUI. I found some solutions throughout some searches and came up with a close solution. But when i access from the MainActivity
class it says the JSONObject
is null. What have i dont wrong here? Is this the best way to do this ?
Following is my AsyncTask
Class
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
* Created by Nisal on 13-Sep-17.
*/
public class GetStationsAPICall extends AsyncTask<String, Void, JSONObject> {
Context ctx;
JSONObject responseObj;
String result;
public interface AsyncResponse {
void processFinish(JSONObject output);
}
public AsyncResponse delegate = null;
public GetStationsAPICall(AsyncResponse delegate){
this.delegate = delegate;
}
// GetStationsAPICall(Context ctx){
// this.ctx=ctx;
// }
@Override
protected JSONObject doInBackground(String... params) {
String method = params[0];
if(method.equals("getStations")){
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://api.gate.com/?lang=en";
HttpGet httpGet = new HttpGet(getURL);
httpGet .setHeader("Authorization", "Bearer 690");
HttpResponse response = client.execute(httpGet);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
//parse response.
Log.e("Response", EntityUtils.toString(resEntity));
// return "Successfully Connected!";
}else{
// return "Connection Failed!";
}
} catch (Exception e) {
e.printStackTrace();
// return "Connection Failed!";
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(JSONObject obj) {
delegate.processFinish(obj);
}
}
Following is my MainActivity
Class
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.json.JSONObject;
public class MainActivity extends Activity implements GetStationsAPICall.AsyncResponse{
Button btnSearch;
String method = "getStations";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetStationsAPICall getStations = new GetStationsAPICall(this);
new GetStationsAPICall(this).execute(method);
}
public void searchClicked(View view){
Toast.makeText(MainActivity.this,"Search Clicked",Toast.LENGTH_SHORT).show();
}
@Override
public void processFinish(JSONObject output) {
Toast.makeText(MainActivity.this,"ProcessFinish",Toast.LENGTH_SHORT).show();
if(output != null){
Toast.makeText(MainActivity.this,"not null",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this," null",Toast.LENGTH_SHORT).show();
}
}
}
I can get the JSONObject
within the AsyncTask
class, But when i try to pass it to the MainActivity
class and use it there. The JSONObject becomes null
. What have I done wrong here?