简体   繁体   English

未为该类型定义方法DataOutputStream(OutputStream)

[英]The method DataOutputStream(OutputStream) is undefined for the type

I followed a lot of tutorials to make progress with this project. 我遵循了许多教程,以使该项目取得进展。 Now I am following a tutorial to create a Google cloud messaging server using JSON and Jackson library. 现在,我正在按照教程使用JSON和Jackson库创建Google云消息服务器。

I somehow got the right Jackson library of all the libraries on the internet. 我以某种方式获得了互联网上所有图书馆的正确的Jackson图书馆。 But an error appeared which is the title of this question. 但是出现了一个错误,这是此问题的标题。

This is that code: 这是该代码:

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();
        }
    }
}

I don't know how to fix this one, I've looked for answers but there isn't any answer. 我不知道该如何解决,我一直在寻找答案,但没有任何答案。

your are missing new keyword 您缺少new关键字

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

replace with 用。。。来代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM