【问题标题】:Error pasting data org.json.JSONException粘贴数据时出错 org.json.JSONException
【发布时间】:2016-04-07 04:09:52
【问题描述】:

这是json代码,httpurlconnection从json获取数据

protected Void doInBackground(Void...params){
        InputStream is=null;

        String result="";

        String urlDate="http://pangkortourism.ga/fyp/player.php";

        BufferedReader reader=null;

        try{

            URL urlp=new URL(urlDate);

            try {
                HttpURLConnection c = (HttpURLConnection) urlp.openConnection();
                c.setRequestMethod("GET");
                c.setReadTimeout(10000);
                c.connect();
                reader = new BufferedReader(new InputStreamReader(c.getInputStream(), "UTF-8"));

                StringBuilder buf = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    buf.append(line);
                }

                result = buf.toString();
            }
            finally {
                if (reader != null) {
                    reader.close();
                }
            }

        } catch (IOException e) {


            Log.e("MalformedURLException", "MalformedURLException " + e.toString());
        }



        //parse json data

        try{

            // Remove unexpected characters that might be added to beginning of the string

                    result=result.substring(result.indexOf("["));

            JSONArray jArray =new JSONArray(result);

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

                JSONObject json_data = jArray.getJSONObject(i);

                Product p=new Product();

                p.setid(json_data.getString("id"));

                p.setitem(json_data.getInt("item"));
                records.add(p);
            }

        }

        catch(Exception e){

            Log.e("ERROR", "Error pasting data "+e.toString());
        }
        return null;

    }

mysql数据库数据

json 代码

[
    {"0":"1","id":"1","1":"pizza","item":"pizza"},
    {"0":"2","id":"2","1":"burger","item":"burger"}
]

android studio 中的错误

04-07 11:58:12.527 23232-23303/com.khor.newtry6 E/ERROR: 粘贴错误 数据 org.json.JSONException:值披萨类型的项目 java.lang.String 无法转换为 int 04-07 11:58:12.542 23232-23232/com.khor.newtry6 E/大小:0

【问题讨论】:

  • json_data.getInt("item"));项目不是数字
  • 我先回答了问题,然后复制粘贴:P

标签: android mysql json


【解决方案1】:

从您的代码中删除以下行,因为它表示 json arrya

result=result.substring(result.indexOf("["));

然后在尝试之后它可能会起作用。 并将项目 p.setitem(json_data.getInt("item")); 更改为 p.setitem(json_data.getString("item"));

【讨论】:

    【解决方案2】:

    “item”字段是字符串而不是整数,因此

    以下更改:

    p.setitem(json_data.getString("item"));
    

    【讨论】:

      【解决方案3】:

      尝试使用 p.setitem(json_data.getString("item"));而不是

      p.setitem(json_data.getInt("item"));

      并在方法 p.setitem() 中更改参数数据类型;到字符串..

      即p.setitem() 的签名;方法应该是:

      public void setitem(String item){

      this.item =item;

      }

      【讨论】:

        猜你喜欢
        • 2018-09-27
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-05
        • 1970-01-01
        相关资源
        最近更新 更多