簡體   English   中英

未為該類型定義方法DataOutputStream(OutputStream)

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

我遵循了許多教程,以使該項目取得進展。 現在,我正在按照教程使用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();
        }
    }
}

我不知道該如何解決,我一直在尋找答案,但沒有任何答案。

您缺少new關鍵字

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

用。。。來代替

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM