我的活动有四个片段。我想从此活动访问asyncTask的变量。该变量在asyncTask的onPostExecute方法中实例化。
我尝试在此类中创建方法并在活动中访问它。但是我得到了空对象。
不知道出了什么问题。
的AsyncTask
public class GetProfileAsyncTask extends AsyncTask<String, Void, JSONObject> {
String api;
JSONObject jsonParams;
private Context context;
public static JSONObject profile;
public GetProfileAsyncTask(Context context) {
this.context = context;
}
@Override
protected JSONObject doInBackground(String... params) {
try {
api = context.getResources().getString(R.string.server_url) + "api/user/getprofile.php";
jsonParams = new JSONObject();
String username = params[0]; // params[0] is username
jsonParams.put("user", username);
ServerRequest request = new ServerRequest(api, jsonParams);
return request.sendRequest();
} catch(JSONException je) {
return Excpetion2JSON.getJSON(je);
}
} //end of doInBackground
@Override
protected void onPostExecute(JSONObject response) {
super.onPostExecute(response);
Log.e("ServerResponse", response.toString());
try {
int result = response.getInt("result");
profile = response.getJSONObject("profile");
String message = response.getString("message");
if (result == 0 ) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
//code after getting profile details goes here
} else {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
//code after failed getting profile details goes here
}
} catch(JSONException je) {
je.printStackTrace();
Toast.makeText(context, je.getMessage(), Toast.LENGTH_LONG).show();
}
} //end of onPostExecute
public static JSONObject getProfile()
{
return profile;
}
}
活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Profile");
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
profileAsyncTask = new GetProfileAsyncTask(this);
GetProfileAsyncTask task = new GetProfileAsyncTask(this);
profile = GetProfileAsyncTask.getProfile();
task.execute("sid");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mTitles,mNumbOfTabs);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabs = (SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tab_scroll_color);
}
});
mTabs.setViewPager( mViewPager);
}
编辑:试过这仍然是空的。
如何从JSON对象访问参数?
在asyncTask中:
public void onTaskCompleted(JSONObject response) {
profile = response;
}
的活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Profile");
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
profileAsyncTask = new GetProfileAsyncTask(this);
GetProfileAsyncTask task = new GetProfileAsyncTask(this);
task.execute("sid");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mTitles,mNumbOfTabs);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabs = (SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tab_scroll_color);
}
});
mTabs.setViewPager( mViewPager);
}
@Override
public void onTaskCompleted(JSONObject response) {
profile = response;
}
将配置文件对象设为null。请帮忙。谢谢。
答案 0 :(得分:0)
在活动级别创建任务外部的字段。在onPostExecute中更新它。