gpt4 book ai didi

android - 如何在 Asyntask 完成后保留数据值并将数据返回到主 UI?

转载 作者:太空狗 更新时间:2023-10-29 14:21:50 24 4
gpt4 key购买 nike

我正在尝试从 AsyncTask 类获取值以返回到主 UI,即使在 AsyncTask 类完成后也是如此。来自 onPostExectute() 的字符串值将被保存到 json_string 以备后用。然而,当我尝试运行这段代码时,它给了我NullPointerException

我已经尽力了 3 天了,仍然不知道如何解决这个问题:(有人请帮忙。我真的很感谢你的帮助。

P.S:我遵循了 http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ 中的教程,但是我做了这个简单的版本来测试。

public class ParsingJSONDataSample extends Activity {

// url to make request
private static String url = "http://api.androidhive.info/contacts/";

// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";

static TextView textId, textName;

// contacts JSONArray
static JSONArray contacts = null;

static InputStream is = null;
static JSONObject jObj = null;
static String json_string;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.json_listview_sample);

textId = (TextView) findViewById(R.id.textView1);
textName = (TextView) findViewById(R.id.textView2);

new ReadJSONWebServiceFeedFromURL().execute(url);

jObj = new JSONObject(json_string);

// Getting Array of Contacts
contacts = jObj.getJSONArray(TAG_CONTACTS);
JSONObject c = contacts.getJSONObject(0);
textId.setText(c.getString(TAG_ID));
textName.setText(c.getString(TAG_NAME));
}
}

//get the JSON webservice from URL
public String getJSONFromUrl(String url) {

StringBuilder sb = new StringBuilder();
String json;
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
int statuscode = statusLine.getStatusCode();

if (statuscode == 200){
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
else{
Log.d("readJSONFeed", "Failed to download file");
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// return JSON String
return json;
}

//Asynctask class to read the JSON webservice in background
private class ReadJSONWebServiceFeedFromURL extends AsyncTask<String, Void, String>{

protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
return getJSONFromUrl(urls[0]);
}

protected void onPostExecute(String result){
json_string = result;
}
}
}

最佳答案

字符串json;将其用作全局

干杯,

关于android - 如何在 Asyntask 完成后保留数据值并将数据返回到主 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16135464/

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