如何用json解析?

时间:2016-05-29 21:21:03

标签: android json listview parsing

我有一个问题,android json从mysql解析,当我尝试执行我的项目时,它显示“org.json.jsonexception在字符0的输入结束”     这是我的解析代码:

protected String doInBackground(String... arg0) {
        for(String url: arg0){
            try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            HttpResponse response = httpclient.execute(httppost);
            inp = response.getEntity().getContent();
        }
        catch (ClientProtocolException e){
            error = "ClientProtocolException: " + e.getMessage();
        }catch (IOException e){
            error = "ClientProtocolException: " + e.getMessage();
        }

        }
        BufferedReader reader;

        try{
            reader = new BufferedReader(new InputStreamReader(inp, "iso-8859-1"),8);
            String line=null;
            while ((line = reader.readLine()) != null){
                txt += line+"\n";
            }
            inp.close();

        }catch (UnsupportedEncodingException e){
            error = "Unsupport Encoding: "+e.getMessage();
        }catch (IOException e){
            error = "Error IO: "+e.getMessage();
        }
        list1 = new ArrayList<Pharm>();

        try{

            JSONArray jArray = new JSONArray(txt);

            for(int i=0;i<jArray.length();i++){
                JSONObject jsonData = jArray.getJSONObject(i);


                Pharm pharm = new Pharm();
                pharm.setNomPharm(jsonData.getString("nomPharm"));
                pharm.setAdressePharm(jsonData.getString("adressePharm"));

                list1.add(pharm);

            }
        }catch (JSONException e){
            error = "Error convert to JSON or Error JSON format: "+ e.getMessage();
        }
        return error;
    }

我需要你的帮助。

1 个答案:

答案 0 :(得分:0)

我会建议Genson这样做。 对于您的gradle文件:

compile 'com.owlike:genson:1.4'

并查看此页面了解其工作原理:

https://owlike.github.io/genson/Documentation/UserGuide/

666