0

I am using HttpURLConnection for POST message to GCM like:

try {
    URL url = new URL(GCM_SERVER_URL);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Authorization", "key=" + apiKey);
    conn.setDoOutput(true);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

    DataOutputStream dataOutputStream = new DataOutputStream(conn.getOutputStream());

    mapper.writeValue(dataOutputStream, content);

    dataOutputStream.flush();
    dataOutputStream.close();

    // Get the response
    int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    System.out.println(response.toString());
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

And now i want to use Retrofit for POST message to GCM
I tried:

@Headers("Content-Type: application/json")
@FormUrlEncoded
@POST("/")
public GCMObject GCMAuthorization(@Header("Authorization") String apiKey,
                                  @Body String data
);

I sent json string in data, But it always failed with this error:

@Body parameters cannot be used with form or multi-part encoding.

I did't found any solution, how can i fix it?

MarsPeople
  • 1,772
  • 18
  • 30

1 Answers1

1

@FormUrlEncoded is used when you want to send form parameters. These parameters are encoded as the body, you can have your own. It does not look like you are using and form parameters, so remove @FormUrlEncoded. Also, I recommend using GSON to convert your POJO to JSON for the @Body. It looks like you are using retrofit 1 and trying to send a raw String. Retrofit will try to JSON encode that for you, which means you'll end up with the object you send wrapped in "...". If you want to send a raw string, take a look at this answer for your options in retrofit 1.

Community
  • 1
  • 1
iagreen
  • 31,470
  • 8
  • 76
  • 90
  • Thank you, a little different question, i send post to GCM adding "Content-Type:..." and "Authorization:key=A..." to "Header", and response is "400 InvalidTokenFormat", there is no solution in internet, HttpURLConnection working fine but retrofit is failed with this error. Is it retrofit encode error or have you any idea about this error? – MarsPeople Nov 15 '15 at 15:24
  • My guess would be you are prepending "key=" when you set the header in the UrlConnection, so make sure to do the same thing with retrofit - `GCMAuthorization.("key="+apiKey, data)` – iagreen Nov 15 '15 at 15:30
  • Did you change your `@Body` to use `TypedInput` as explained in the linked answer? You are going to be sending invalid JSON in the body. – iagreen Nov 15 '15 at 15:39
  • I think you are missing the "to" field in your new implementation. Add it to your `GCMRequest` object. – iagreen Nov 15 '15 at 15:50
  • it's not working with "to" like in doc, it's working with "registration_ids", with HttpURLConnection. But when i change HttpURLConnection -> Retrofit it fails, I did't think it is about Body content or my request parameters. GCM is not accept "Authorization" format and return "400 InvalidTokenFormat", the same key i am using with HttpURLConnection it success, but retrofit failed. Than i think retrofit is wrong encode the header parameter. I am very confused these times, and google did't add any doc about GCM response "400 InvalidTokenFormat"... – MarsPeople Nov 15 '15 at 15:57
  • I would suggest opening another question with the details of the new code and error. I am out of ideas – iagreen Nov 15 '15 at 16:03