简体   繁体   English

如何将php json url解析为android list view,这是代码我没有显示

[英]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.

相关问题 你好,我正在获取 json 数据,但没有显示在列表视图中 - hello i am getting json data but not display in to list view 如何解析JSON对象并将其显示在列表视图中? - How can I parse a JSON object and display it in a list view? 如何从url下载Android文件中的mp3文件并将其保存在SD卡中? 这是我正在使用的代码 - How to download mp3 file in android from a url and save it in SD card?? Here is the code i am using 如何从android中的url解析多个JSON对象和数组? 我正在使用一个例子,我想在那个例子中, - How to parse Multiple JSON Objects and arrays from a url in android? i am using a example i want it in that example, 试图解析json到android上的列表视图,但得到错误 - Trying to parse json to list view on android but getting error 我正在尝试解析JSON feed并将其显示到自定义Listview中,但是我收到了NullPointerException - I am trying to parse JSON feed and display it into a Custom Listview but I am getting NullPointerException 如何解析在线JSON URL并将其显示在Android的ListView中 - How to parse online JSON url and display it on android's listview 我正在通过回收站视图设置静态图像,但我收到空指针异常,这是我的代码和错误 - I am setting static image through recycler view but I am getting null pointer exception here is my code and eror 解析JSON并将对象显示到列表视图 - parse JSON and display object onto list view 如何在Android中解析带有URL图像和文本的XML并在列表视图中对其进行框架 - How to parse XML in android with the url image and text and frame it in a List view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM