【问题标题】:how to parse with json?如何用json解析?
【发布时间】:2016-09-27 15:50:59
【问题描述】:

我在从 mysql 解析 android json 时遇到问题,当我尝试执行我的项目时,它显示“org.json.jsonexception end of input at character 0 of” 这是我的解析代码:

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;
    }

我需要你的帮助。

【问题讨论】:

  • 你能告诉我们返回的JSON吗?
  • 这将是我们解决问题的必须...
  • { 药店:[ { idPharm:“1”,nomPharm:“pharm1”,adressePharm:“hay saada”,lat:“ssssssss”,lon:“ddddddddd”}]}
  • 我用 127.0.0.1 测试它
  • 我必须显示在 listView 中返回的 JSON 的一些键

标签: android json listview parsing


【解决方案1】:

我会推荐 Genson 这样做。 对于您的 gradle 文件:

compile 'com.owlike:genson:1.4'

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

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

666

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多