gpt4 book ai didi

java - 如何获取json字符串的值,然后通过这个函数解析它

转载 作者:行者123 更新时间:2023-12-01 15:40:05 24 4
gpt4 key购买 nike

我有以下方法连接到数据库并返回一个 json 数组。

private String getServerData(String returnString) {

InputStream is = null;

String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1970"));

//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
// JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);


//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());

returnString += result.toString();
}
return returnString;
}

然后我想从外部调用这个方法,但是对于java来说我很陌生,我不知道如何将这个刺痛放入hm对象中,就像注释掉的那样。请参阅下面的代码

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
myBooks = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
List<String> list=new ArrayList<String>();
list.add(getServerData(KEY_121));





//With the help of HashMap add Key, Values of Book, like name,price and icon path
/* hm = new HashMap<String, Object>();
hm.put(BOOKKEY, "Android");
hm.put(PRICEKEY, "Price Rs: 500");
hm.put(IMGKEY, R.raw.android); //i have images in res/raw folder

myBooks.add(hm);


SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox,
new String[]{BOOKKEY,PRICEKEY,IMGKEY}, new int[]{R.id.text1, R.id.text2, R.id.img});

listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

我的目标是获取返回的字符串,并使用 hm 对象将其添加到 myBooks 对象中,就像在注释代码中所做的那样。

谢谢

最佳答案

由于 getServerData() 需要一段时间才能执行(它包含一个 http 请求),因此您无法直接从 UI 线程运行它。 (onCreate() 直接在 UI 线程上,并且 UI 线程永远不应该暂停)。

这意味着您必须创建一个 AsyncTask 并从 AsyncTaskdoInBackground() 调用 getServerData()。您可以在AsyncTaskonPostExecute()中返回显示JSON结果。

这意味着您想要传递 SoftReference 您想要向 AsyncTask 显示结果的位置。您希望将其设为SoftReference,以防在获取 JSON 时发生问题(例如您离开 Activity)。

我建议您阅读 this Android blog article 并使用 the code from the article 创建您自己的项目。一旦启动并运行,将该图像下载器修改为 JSON 下载器。这篇文章应该对您很有帮助,因为它还使用适配器在 ListView 中显示项目。

您的示例在很多方面与文章中的示例不同,但我认为它实际上更简单,因此如果您认真研究文章中的代码,那么您将能够让您的代码随后运行。

引用文献:
<强> AsyncTask
<强> SoftReferences
<强> Multithreading for Performance
<强> Working Code from the Article Above which implements something like you want but w images

关于java - 如何获取json字符串的值,然后通过这个函数解析它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204700/

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