gpt4 book ai didi

java - 用于 Fragment 的 AsyncTask 结果

转载 作者:行者123 更新时间:2023-12-01 09:18:10 25 4
gpt4 key购买 nike

我有一个关于AsyncTask的问题我使用 AsyncTask 获取 Json 名称列表

这是我的AsyncTask

class GetNameAsync extends AsyncTask<String, String, JSONObject> {

JSONParser jsonParser = new JSONParser();
private static final String API_URL = "urlhere :-)";
private static final String TAG_NAMES = "names";
private ProgressDialog pDialog;

@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Attempting loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected JSONObject doInBackground(String... args) {
try {
HashMap<String, String> params = new HashMap<>();
params.put("access", KEY);
params.put("lang", LANG);

Log.e("request", "starting");
JSONObject names_json = jsonParser.makeHttpRequest(API_URL, "GET", params);
return names_json;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject names_json) {
super.onPostExecute(names_json);
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
String names_entry = "";
String mAktienlisteAdapter1 = "";

try {
names_entry = names_json.getString(TAG_NAMES);
} catch (JSONException e) {
e.printStackTrace();
}
}
}

这很好用但是我没有得到的是我得到的结果到外面继续使用

我已经尝试过了这在 onPostExecute

    getFinalResult(String.valueOf(names_entry));

这在我的 fragment 中

public static String RESULT = null;
public void getFinalResult(String string) {
RESULT = string;
}

那么RESULT是空的-,-

已经看过这里,但没有发现任何对我有帮助的东西。如果有人可以帮助我解决我的问题,我会很高兴。

编辑这是我的Fragment.class

public class ThirdFragment extends Fragment {
public static String LANG = null;
public static String KEY = null;
public static String RESULT = null;



class GetNameAsync extends AsyncTask<String, String, JSONObject> {

JSONParser jsonParser = new JSONParser();
private static final String API_URL = "urlhere :-)";
private static final String TAG_NAMES = "names";
private ProgressDialog pDialog;

@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Attempting loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected JSONObject doInBackground(String... args) {
try {
HashMap<String, String> params = new HashMap<>();
params.put("access", KEY);
params.put("lang", LANG);

Log.e("request", "starting");
JSONObject names_json = jsonParser.makeHttpRequest(API_URL, "GET", params);
return names_json;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject names_json) {
super.onPostExecute(names_json);
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
String names_entry = "";
String mAktienlisteAdapter1 = "";

try {
names_entry = names_json.getString(TAG_NAMES);
} catch (JSONException e) {
e.printStackTrace();
}
getFinalResult(String.valueOf(names_entry));

}
}

public void getFinalResult(String string) {
RESULT = string;
}
public void setLang(String string){
LANG = string;
}

public void setKey(String string){
API_KEY = string;
}

public ThirdFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_third, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
fab.setVisibility(View.VISIBLE);

GetNameAsync GetNames = new GetNameAsync();
GetNames.execute();
Log.e("RESULT-ASYNC", RESULT);


}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.main1, menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

if (item.getItemId() == 1) {
Toast.makeText(getActivity(), "Test 1", Toast.LENGTH_LONG).show();
return true;
} else if (item.getItemId() == 2) {
Toast.makeText(getActivity(), "Test 2", Toast.LENGTH_LONG).show();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}

最佳答案

问题是当您尝试记录结果时,您的异步任务尚未完成。

GetNameAsync GetNames = new GetNameAsync();
GetNames.execute();
Log.e("RESULT-ASYNC", RESULT); // At this point the task may not be complete.

GetNameAsync 的方法 onPostExecute 正在 GUI 线程中运行,并且可以访问 Fragment 上的任何内容,例如标签的文本值等。因此请尝试更新您的 GUI( fragment )在您的 onPostExecute 中,其中包含您从 name_entry 获得的值

关于java - 用于 Fragment 的 AsyncTask 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40370714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com