我有一個JSONObject
從AsyncTask
類中的API調用接收。我想將該JSON對象傳遞給我的MainActivity
類,以在GUI中顯示JSONObject
數據。我在一些搜索中發現了一些解決方案,並提出了一個解決方案。但是,當我從MainActivity
類訪問它說JSONObject
爲空。我在這裏沒有錯?這是做這件事的最好方法嗎?Android:如何將JSON對象從AsncTask類傳遞到主類
以下是我AsyncTask
類
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);
}
}
以下是我MainActivity
類
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();
}
}
}
我可以AsyncTask
類中獲得JSONObject
,但是當我試圖把它傳遞給MainActivity
類在那裏使用它。 JSONObject變成null
。我在這裏做錯了什麼?
因爲你在doInBackground中只返回null而不是其他東西嗎? –
查看我的回答 –