gpt4 book ai didi

java - 从Activity传递参数到doInBackground方法,异步任务

转载 作者:行者123 更新时间:2023-12-02 07:16:17 26 4
gpt4 key购买 nike

我试图将变量“chartUrl”从 JsonActivity 传递到 doInBackground 方法,如下所示。我试图改变论点,但没有成功。有谁知道该怎么做吗?

提前致谢

代码:

公共(public)类 JsonActivity 扩展了 ListActivity{

 private ProgressDialog progressDialog;

// JSON Node names

private static final String TAG_RANK = "rank";
private static final String TAG_NAME = "name";
String chartUrl;

String[] urlNames = new String[] {
"myurls"

};

// chartItemList holds the chart items
ArrayList<HashMap<String, String>> chartItemList = new ArrayList<HashMap<String,
String>>();
JsonParser Parser = new JsonParser();

JSONArray chartItems = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listlayout);

//Get the bundle from other activity
Bundle bundle = getIntent().getExtras();

//Extract chart index from the bundle
int chartIndex = bundle.getInt("chartIndex");
String chartUrl = urlNames[chartIndex];


//Check if the user has a connection

ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
Toast.makeText(this, "Please check your connection and try again.",
Toast.LENGTH_SHORT).show();
}

//if positive, fetch the articles in background
else new getChartItems().execute(chartUrl);
}

//else show toast
else {
Toast.makeText(this, "Please check your connection and try again.",
Toast.LENGTH_SHORT).show();
}

}


class getChartItems extends AsyncTask<String, String, String> {

// Shows a progress dialog while setting up the background task
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(JsonActivity.this);
progressDialog.setMessage("Loading chart...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}

//Gets the json data for chart items data and presents it in a list view
@Override
protected String doInBackground(String... args) {


String json = Parser.getJSONFromUrl(chartUrl);

String rank;

String name;

try{

chartItems = new JSONArray(json);

JSONObject json_data=null;

for(int i=0;i<chartItems.length();i++){

json_data = chartItems.getJSONObject(i);
rank=json_data.getString("rank");
name=json_data.getString("name");


HashMap<String, String> hashMap = new HashMap<String, String>();
// adding each child node to HashMap key => value
hashMap.put(TAG_RANK, rank);
hashMap.put(TAG_NAME, name);

// adding HashMap to ArrayList
chartItemList.add(hashMap);

}


;
}

catch (JSONException e) {
e.printStackTrace();
}

runOnUiThread(new Runnable() {
public void run() {
System.out.println(chartItemList);

//updates the list view with the parsed items
ListAdapter adapter = new SimpleAdapter(JsonActivity.this, chartItemList,
R.layout.list_item,
new String[] {TAG_RANK,TAG_NAME, }, new int[]

{R.id.rank ,R.id.name});
setListAdapter(adapter);
}
});
return null;
}

//Removes the progress dialog when the data has been fetched
protected void onPostExecute(String args) {
progressDialog.dismiss();
}
}


}

最佳答案

使用

String json = Parser.getJSONFromUrl(args[0]);

而不是

String json = Parser.getJSONFromUrl(chartUrl);

用于在 doInBackground 方法中获取 chartUrl 值,如果 getChartItems 是 Activity 的内部类,则只需将 chartUrl 声明为类级别变量即可访问它全类同学

关于java - 从Activity传递参数到doInBackground方法,异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14911633/

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