how to parse huge JSON data froma url or restful service in android?without GSON

不想你离开。 提交于 2019-12-04 19:31:08

You can use gson library. There is an example here!

As it is nested json and You have not specified which fields do you need its difficult to answer so Use this class as a refrence class use method from where You need data to be displayed, It will return the first object of your array.

public class Parser {

    public String getData() {
        String content = null;

        String result = "";
        InputStream is = null;

        // http get
        try {
            HttpClient httpclient = new DefaultHttpClient();

            String webUrl = "your url";

            HttpGet httppost = new HttpGet(webUrl);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            Log.d("LOGTAG", "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();
            Log.d("LOGTAG", "Result :" + result);
        } catch (Exception e) {
            Log.d("LOGTAG", "Error converting result " + e.toString());
        }

        // parse json data

        try {

            JSONArray jarray = new JSONArray(result);
            JSONObject jobj = jarray.getJSONObject(jarray.getInt(0));
            content = jobj.getString("key");//key=name of the field you require
        } catch (Exception e) {

            Log.d("LOGTAG", e.toString());

        }
        return content;
    }
}

I saw your comment ... you do not want to change your code. but parsing a json yourself is pretty slow in comparison to using Gson..and use of gson is so simple you just need to create simple pojo classes and it will convert whole json into java object which you can use anywhere. this is also a neat way of doing it.here is an example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!