在Android应用中,我想将JSONObject从doInBackground()
方法返回到onPostExecute()
方法
这是代码:
private class AddAsyncTask extends AsyncTask<String, Void, String>
{
JSONObject jsonObjRecv;
String result;
@Override
protected JSONObject doInBackground(JSONObject... params) {
AssetObj assetObj = new AssetObj();
assetObj.setAssetName(txtname.getText().toString());
assetObj.setMobileNo(txtmobile.getText().toString());
assetObj.setOwnerId(myApp.getOwnerId());
assetObj.setStartTime(startTime.getText().toString());
assetObj.setEndTime(endTime.getText().toString());
assetObj.setInterval(interval.getText().toString());
JSONObject jsonObjRecv = SyncService.AddNewAssetRequest(assetObj);
return jsonObjRecv;
}
protected void onPostExecute(JSONObject obj){
if(obj != null)
{
//do something
}
我试过这段代码我收到了错误。是否可以将JSONObject从doInBackground()
方法返回到onPostExecute()
方法?
答案 0 :(得分:4)
<强>编辑:强>
这可以帮助你,
private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject>
{
JSONObject jsonObjRecv;
String result;
@Override
protected JSONObject doInBackground(String... params) {
AssetObj assetObj = new AssetObj();
assetObj.setAssetName(txtname.getText().toString());
assetObj.setMobileNo(txtmobile.getText().toString());
assetObj.setOwnerId(myApp.getOwnerId());
assetObj.setStartTime(startTime.getText().toString());
assetObj.setEndTime(endTime.getText().toString());
assetObj.setInterval(interval.getText().toString());
JSONObject jsonObjRecv = SyncService.AddNewAssetRequest(assetObj);
}
protected void onPostExecute(JSONObject obj){
if(obj != null)
{
//do something
}
这是明确的,
private class AddAsyncTask extends AsyncTask<What type of input you need to pass to doInBackground(), Void, What type of return value you need to return to onPostExcute()>
可能你不需要在方法声明中更改返回值和参数。
只需创建以下行
private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject>
将根据您在
中提到的参数和返回类型自动创建方法private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject>
答案 1 :(得分:2)
将AsyncTask<T1, T2, T3>
传递T3
作为JSONObject
答案 2 :(得分:2)
好的,现在仔细看看,
private class AddAsyncTask extends AsyncTask<String, Void, String>
在AsyncTask中,第三个参数为String
,因此请将其更改为JSONObject
。
等
private class AddAsyncTask extends AsyncTask<String, Void, JSONObject>
答案 3 :(得分:2)
而不是
private class AddAsyncTask extends AsyncTask<String, Void, String>
更改为
private class AddAsyncTask extends AsyncTask<String, Void, JsonObject>
实际代码
private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject>
{
JSONObject jsonObjRecv;
String result;
@Override
protected JSONObject doInBackground(JSONObject... params) {
AssetObj assetObj = new AssetObj();
assetObj.setAssetName(txtname.getText().toString());
assetObj.setMobileNo(txtmobile.getText().toString());
assetObj.setOwnerId(myApp.getOwnerId());
assetObj.setStartTime(startTime.getText().toString());
assetObj.setEndTime(endTime.getText().toString());
assetObj.setInterval(interval.getText().toString());
JSONObject jsonObjRecv = SyncService.AddNewAssetRequest(assetObj);
}
protected void onPostExecute(JSONObject obj){
if(obj != null)
{
//do something
}
}
}
AsyncTask&lt;参数,进展,结果&gt;