Reputation: 1824
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?
Upvotes: 0
Views: 575
Reputation: 32016
@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.
Upvotes: 1