0

When i passing json data from Android to server , its howing following error at server at the time of parsing.org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

The servlet code is :

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 portion is :

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

            }

I'm passing from client as :

[
{"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}
]

when i print Json data in server its look like

 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
5
  • post full logcat messages or what u are getting in System.out.println(sb.toString()) line in log Commented Mar 19, 2013 at 5:47
  • stackoverflow.com/questions/4773663/… Commented Mar 19, 2013 at 5:48
  • How are you printing the json? Could you paste code? Commented Mar 19, 2013 at 5:54
  • @javapirate in android nameValuePairs.add(new asicNameValuePair("str", new Gson().toJson(strlist))); Commented Mar 19, 2013 at 5:56
  • @JinjuJoseph The problem is that the json string is not escaped of special characters when you receive them in doProcess method(). Ensure that you escape all the { [ : before you pass them to this method Commented Mar 19, 2013 at 8:06

1 Answer 1

1

You can try below code for parsing 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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

i have issue at JSONObject json = new JSONObject(jsonstring); i couldn't parse my string to JSONObject, pls see the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.