user2842224
user2842224

Reputation: 31

Geeting JSON Eception While connecting GCM through Java

I'm trying to connect to GCM via JAVA code.

for application/x-www-form-urlencoded;charset=UTF-8 it's working fine, but when I'm using Content-Type: application/json I get JSON_PARSING_ERROR: Unexpected character (r) at position 0.

My code is:

     List<NameValuePair> formparams = new ArrayList<NameValuePair>();

                formparams.add(new BasicNameValuePair("registration_id", 
GCM_ID));
                formparams.add(new BasicNameValuePair("data.message", 
"this is data mesg"));
                // UrlEncodedFormEntity entity = new 
UrlEncodedFormEntity(formparams,
                // "UTF-8");


                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(REQUEST_URL);

                // ADDING DATA IN TO HEADER OF URL

                httpPost.setHeader("Authorization",
                        "key=AIzaSyBmy5WfZMuO-BGMaWEtVhI1f-2gqyN-1h0");
                /*httpPost.setHeader("Content-Type",
                        "application/x-www-form-urlencoded;charset=UTF-
8");*/
                httpPost.setHeader("Content-Type",
                        " application/json");                // not   
working


                try {
                    httpPost.setEntity(new 
UrlEncodedFormEntity(formparams, "utf-8"));

                    // RETRIEVING RESPONSE FROM URL
                    System.out.println("1");
                    HttpResponse response= httpclient.execute(httpPost);
                    System.out.println("2");
                    str = 
inputStreamToString(response.getEntity().getContent()).toString();
                    System.out.println("3");
                    System.out.println(str+" "+response);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

The code to parse the response is:

private StringBuilder inputStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();
    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    // Read response until the end
    try {
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (IOException e) {

        Log.e("inputStreamToString", "during convert");
        e.printStackTrace();
    }
    // Return full string
    return total;
        }

Upvotes: 3

Views: 1938

Answers (1)

Eran
Eran

Reputation: 393841

Of course it's not working. You are sending application/x-www-form-urlencoded content while specifying the content type as application/json. formparams is not a JSON String.

The JSON String should look like this:

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : { 
    "message" : "this is data mesg"
  },
}

Try to use StringEntity instead of UrlEncodedFormEntity:

Something like this should work, assuming jsonString contain's a String in the correct format:

httpPost.setEntity(new StringEntity(jsonString, ContentType.APPLICATION_JSON));

Upvotes: 3

Related Questions