0

Androidからサーバーにjsonデータを渡すと、解析時にサーバーで次のエラーが発生します。org.json.JSONException: JSONObject テキストは 1 の '{' で始まる必要があります [文字 2 行 1]

サーブレット コードは次のとおりです。

protected void doProcess(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();

        String sCurrentLine;
        StringBuilder sb = new StringBuilder();

        while ((sCurrentLine = br.readLine()) != null) {
            sb.append(sCurrentLine);
        }

        System.out.println(sb.toString().substring(4));

        try {
            JSONArray jArray = new JSONArray(sb.toString().substring(4));
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                json_data.getString("dwsList");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

Android の部分は次のとおりです。

ArrayList<STRModel> strlist = StrDbHelper
                    .getPendingStrlist(Splash.this);
            System.out.println("--------Inside------" + strlist.size());

            if (strlist != null) {
                String URL = "http://ip:8080/Admin/upload";
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(URL);

                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            2);
                    nameValuePairs.add(new BasicNameValuePair("str", new Gson()
                            .toJson(strlist)));


                    System.out.println(new Gson().toJson(strlist));



                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpclient.execute(httppost);

                    Log.i("Response", response.getEntity().getContent()
                            .toString());

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }

            }

私はクライアントから次のように渡しています:

[
{"dwsList":
[
{"farmer_name":"JINJU","dws_date":"2013-03-08","farmer_code":"10004","dws_num":"53","dws_id":4,"bag_weight":3000.0,"net_weight":2000.0,"selected":false,"str_id":0,"totalBags":20},
{"farmer_name":"KANNAN","dws_date":"2013-03-08","farmer_code":"10005","dws_num":"54","dws_id":5,"bag_weight":1500.0,"net_weight":1000.0,"selected":false,"str_id":0,"totalBags":10}
],
"str_total_weight":4000.0,"str_date":"2013-03-08","str_number":"1003","str_id":4,"str_total_bag":0,"status":0,"selected":false}
]

サーバーでJsonデータを印刷すると、次のようになります

 str=%5B%7B%22dwsList%22%3A%5B%7B%22farmer_name%22%3A%22JINJU%22%2C%22dws_date%22%3A%222013-03-08%22%2C%22farmer_code%22%3A%2210004%22%2C%22dws_num%22%3A%2253%22%2C%22dws_id%22%3A4%2C%22bag_weight%22%3A3000.0%2C%22net_weight%22%3A2000.0%2C%22selected%22%3Afalse%2C%22str_id%22%3A0%2C%22totalBags%22%3A20%7D%2C%7B%22farmer_name%22%3A%22KANNAN%22%2C%22dws_date%22%3A%222013-03-08%22%2C%22farmer_code%22%3A%2210005%22%2C%22dws_num%22%3A%2254%22%2C%22dws_id%22%3A5%2C%22bag_weight%22%3A1500.0%2C%22net_weight%22%3A1000.0%2C%22selected%22%3Afalse%2C%22str_id%22%3A0%2C%22totalBags%22%3A10%7D%5D%2C%22str_total_weight%22%3A4000.0%2C%22str_date%22%3A%222013-03-08%22%2C%22str_number%22%3A%221003%22%2C%22str_id%22%3A4%2C%22str_total_bag%22%3A0%2C%22status%22%3A0%2C%22selected%22%3Afalse%7D%5D
4

1 に答える 1

1

JSONを解析するための以下のコードを試すことができます

{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea, Republic of"},
{"countryCode":"us","countryName":"United States"},
{"countryCode":"jp","countryName":"Japan"},
{"countryCode":"cn","countryName":"China"},
{"countryCode":"in","countryName":"India"}
]
}

public static ArrayList<Country> ParseJson(String jsonstring) {

    ArrayList<Country> arrCountries = new ArrayList<Country>();

    String status;
    String message = "";
    try {


        JSONObject json = new JSONObject(jsonstring);

        status = json.getString("result");

        if (status.equalsIgnoreCase("success")) {


            JSONArray nameArray = json.names();
            JSONArray valArray = json.toJSONArray(nameArray);

            JSONArray valArray1 = valArray.getJSONArray(1);

            valArray1.toString().replace("[", "");
            valArray1.toString().replace("]", "");

            int len = valArray1.length();

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

                Country country = new Country();
                JSONObject arr = valArray1.getJSONObject(i);

                country.setCountryCode(arr.getString("countryCode"));
                country.setCountryName(arr.getString("countryName"));
                arrCountries.add(country);
            }
        }

    } catch (JSONException e) {
        Log.e("JSON", "There was an error parsing the JSON", e);
    }
    return arrCountries;
}
于 2013-03-19T05:59:29.150 に答える