POST 到 Google 云消息与改造
POST to Google Cloud Messaging with Retrofit
我正在使用 HttpURLConnection for POST message to GCM,例如:
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();
}
现在我想使用 Retrofit 将 POST 消息发送给 GCM
我试过:
@Headers("Content-Type: application/json")
@FormUrlEncoded
@POST("/")
public GCMObject GCMAuthorization(@Header("Authorization") String apiKey,
@Body String data
);
我在 数据 中发送了 json 字符串,但它总是失败并出现此错误:
@Body parameters cannot be used with form or multi-part encoding.
我没有找到任何解决方案,我该如何解决?
@FormUrlEncoded
用于发送表单参数。这些参数被编码为正文,您可以拥有自己的。它看起来不像你在使用和表单参数,所以删除 @FormUrlEncoded
。此外,我建议使用 GSON 将您的 POJO 转换为 JSON 以用于 @Body
。看起来您正在使用改造 1 并尝试发送原始 String
。 Retrofit 将尝试 JSON 为您编码,这意味着您最终会收到包含在“...”中的发送对象。如果您想发送原始字符串,请查看 at this answer 改造 1 中的选项。
我正在使用 HttpURLConnection for POST message to GCM,例如:
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();
}
现在我想使用 Retrofit 将 POST 消息发送给 GCM
我试过:
@Headers("Content-Type: application/json")
@FormUrlEncoded
@POST("/")
public GCMObject GCMAuthorization(@Header("Authorization") String apiKey,
@Body String data
);
我在 数据 中发送了 json 字符串,但它总是失败并出现此错误:
@Body parameters cannot be used with form or multi-part encoding.
我没有找到任何解决方案,我该如何解决?
@FormUrlEncoded
用于发送表单参数。这些参数被编码为正文,您可以拥有自己的。它看起来不像你在使用和表单参数,所以删除 @FormUrlEncoded
。此外,我建议使用 GSON 将您的 POJO 转换为 JSON 以用于 @Body
。看起来您正在使用改造 1 并尝试发送原始 String
。 Retrofit 将尝试 JSON 为您编码,这意味着您最终会收到包含在“...”中的发送对象。如果您想发送原始字符串,请查看 at this answer 改造 1 中的选项。