我正在从Google商家信息中检索我的JSON响应,并且我想在ListView中的另一个Acivity中显示它。我想要做的是将Object从一个活动传递到另一个活动,但它似乎不起作用。
这是我的活动的一部分,我试图传递JSON:
public class GetNearbyPlacesList extends AsyncTask<Object, String, String> {
String googlePlacesData;
String url;
List<HashMap<String, String>> nearbyPlacesList = null;
private Intent context;
private SharedPreferences.Editor intent;
@Override
protected String doInBackground(Object... params) {
try {
Log.d("GetNearbyPlacesData", "doInBackground entered");
url = (String) params[0];
DownloadUrl downloadUrl = new DownloadUrl();
googlePlacesData = downloadUrl.readUrl(url);
Log.d("GooglePlaceData", googlePlacesData);
Log.d("GooglePlacesReadTask", "doInBackground Exit");
} catch (Exception e) {
Log.d("GooglePlacesReadTask", e.toString());
}
return googlePlacesData;
}
protected void onPostExecute(String result) {
Log.d("GooglePlacesReadTask", "onPostExecute Entered");
ListParser listParser = new ListParser();
nearbyPlacesList = listParser.parse(result);
Log.d("List of restaurants", nearbyPlacesList.toString());
Log.d("GooglePlacesReadTask", "onPostExecute Exit");
Intent intent = new Intent(GetNearbyPlacesList.this,ListRestActivity.class);
intent.putExtra("deal", nearbyPlacesList.toString());
startActivity(intent);
}
这是我想将JSON传递给的类:
public class ListRestActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
ListView lv;
double latitude;
double longitude;
private int PROXIMITY_RADIUS = 10000;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listrest);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
//Check if Google Play Services Available or not
if (!CheckGooglePlayServices()) {
Log.d("onCreate", "Finishing test case since Google Play Services are not available");
finish();
}
else {
Log.d("onCreate","Google Play Services available.");
}
String Restaurant = "restaurant";
String url = getUrl(latitude, longitude, Restaurant);
Object[] DataTransfer = new Object[1];
DataTransfer[0] = url;
GetNearbyPlacesList getNearbyPlacesList = new GetNearbyPlacesList();
getNearbyPlacesList.execute(DataTransfer);
}
答案 0 :(得分:0)
在第二个活动中,您可以使用
访问意图的JSON字符串getIntent().getStringExtra("deal");
然后,如果需要,您可以将其转换回JSON对象。
<强>更新强>
您在Async任务中的Intent构造函数中收到错误的原因是您没有将有效的上下文作为参数传递。你的上下文属性是Intent类型,它应该是Context,所以修复如下:
private Context mContext;
将构造函数添加到GetNearbyPlacesList中,如此
public GetNearbyPlacesList(Context context) {
mContext = context
}
在实例化时传递一个上下文,这是您最有可能的第一个活动。
那么当您在异步任务类中构造该意图时,您可以这样做:
Intent intent = new Intent(mContext, ListRestActivity.class)