gpt4 book ai didi

java - 该类型的方法 DataOutputStream(OutputStream) 未定义

转载 作者:行者123 更新时间:2023-12-01 11:14:03 27 4
gpt4 key购买 nike

我遵循了很多教程来使这个项目取得进展。现在,我正在按照教程使用 JSON 和 Jackson 库创建 Google 云消息传递服务器。

我以某种方式在互联网上的所有图书馆中找到了正确的 jackson 图书馆。但是出现了一个错误,这就是这个问题的标题。

这就是代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.jackson.map.ObjectMapper;


public class POST2GCM {

public static void post(String apiKey, Content content){
try{
//1. url
URL url = new URL("https://android.googleapis.com/gcm/send");
//2. open connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//3. specify POST method
conn.setRequestMethod("POST");
//4.set the headers
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key="+apiKey);

conn.setDoOutput(true);
//5. add json data into POST request body
//5.1 use jackson object mapper to convert contnet object into JSON
ObjectMapper mapper = new ObjectMapper();
//5.2 get connection stream
DataOutputStream wr = DataOutputStream(conn.getOutputStream());
//5.3 copy content "JSON" into
mapper.writeValue(wr, content);
//5.4 send the request
wr.flush();
//5.5
wr.close();
//6. 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();
//7. print result
System.out.println(response.toString());
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

我不知道如何解决这个问题,我一直在寻找答案,但没有任何答案。

最佳答案

您缺少关键字

     //5.2 get connection stream 
DataOutputStream wr = DataOutputStream(conn.getOutputStream());

替换为

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

关于java - 该类型的方法 DataOutputStream(OutputStream) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com