[英]how to parse php json url into android list view here is to code i am getting no display
I am parsing json from the following url 我正在从以下网址解析json
http://www.kaverisoft.com/careers/assignments/android/a1.php http://www.kaverisoft.com/careers/assignments/android/a1.php
with the following code 用下面的代码
public class MainActivity extends ListActivity { 公共类MainActivity扩展了ListActivity {
/** Called when the activity is first created. */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter(
this,android.R.layout.simple_list_item_1,
this.populate()));
}
private ArrayList<String> populate() {
ArrayList<String> items = new ArrayList<String>();
try {
URL url = new URL
("http://www.kaverisoft.com/careers/assignments/android/a1.php");
HttpURLConnection urlConnection =
(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String next;
while ((next = bufferedReader.readLine()) != null){
JSONArray ja = new JSONArray(next);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
items.add(jo.getString("text"));
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return items;
}
} }
The logcat shows that org.json.JSONException value text not found Logcat显示未找到org.json.JSONException值文本
but i am not able to populate any data to listview,but if i use direct json format url i can get data populated please help. 但是我无法将任何数据填充到listview,但是如果我使用直接json格式的url,我可以填充数据,请帮忙。
the output is like the image below. 输出如下图所示。
Please help me solve this . 请帮我解决这个问题。
As far as I can see, you have 3 kinds of objects in the list: camera, book and music. 据我所知,列表中有3种对象:相机,书籍和音乐。 For each of them you'll have to use different parsing.
对于它们中的每一个,您都必须使用不同的解析。 First you should detect what kind of objects the current element of the array is.
首先,您应该检测数组的当前元素是哪种对象。
For example: 例如:
if (jo.has("book")) {
JSONObject jsonBook = jo.getJSONObjcejt("book");
// continue with parsing book stuff
} else if (jo.has("camera"){
// same, but for camera
} else if (jo.has("music") {
// same, but for music
}
Then you can populate the list with books, music and camera object you parsed. 然后,您可以使用解析的书籍,音乐和相机对象填充列表。 If you want smarter parsing, have a look at gson .
如果您想更智能地分析,请查看gson 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.