无法解析方法create'com.google.common.net.MediaType,java.lang.String)
问题描述:
我想使用OkHttp 3将设备发送到设备Firebase通知,但发布JSON时出现以下错误:无法解析方法create'com.google.common.net.MediaType,java.lang.String)
cannot resolve method create 'com.google.common.net.MediaType,java.lang.String)
这里是我的代码:
final String legacyServerKey = "";
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
JSONObject dataJson = new JSONObject();
dataJson.put("body", "Hi this is sent from device to device");
dataJson.put("title", "dummy title");
json.put("notification", dataJson);
json.put("to", reg_token);
RequestBody body = RequestBody.create(JSON, json.toString());
Request request = new Request.Builder()
.header("Authorization", "key=" + legacyServerKey)
.url("https://fcm.googleapis.com/fcm/send")
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
String finalResponse = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
答
它看起来像你进口com.google.common.net.MediaType
。您需要okhttp3.MediaType