2012-01-29 90 views
1

我跟隨了UA教程並獲得了我的APID,併成功地在我的Android設備上收到了測試推送消息。城市飛艇推碼

現在我想要做的下一件事就是使用JAVA定位我的設備併發送推送消息。 從我目前看到的最好的方式來實現這一點是使用他們的Web API。

但是當我試圖發送一個消息後,我總是得到以下錯誤:

產生java.io.IOException:服務器返回的HTTP響應代碼:401網址:https://go.urbanairship.com/api/push/ 在sun.net .www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at com.Sender.Sender.main(Sender.java:56)

這是我使用的代碼:

package com.Sender; 

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.net.Authenticator; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

public class Sender { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    try { 
     String responseString = ""; 
     String outputString = ""; 
     String username = "MWMoRVhmRXOG6IrvhMm-BA"; 
     String password = "ebsJS2iXR5aMJcOKe4rCcA"; 

     MyAuthenticator ma= new MyAuthenticator(username, password); 

     Authenticator.setDefault(ma); 

     URL url = new URL("https://go.urbanairship.com/api/push/"); 
     URLConnection urlConnection = url.openConnection(); 
     HttpURLConnection httpConn = (HttpURLConnection) urlConnection; 
     ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
     String APID = "23eef280-19c8-40fc-b798-788f50e255a2"; 

     String postdata = "{\"android\": {\"alert\": \"Hello from JAVA!\"}, \"apids\": [\""+APID+"\"]}"; 
     byte[] buffer = new byte[postdata.length()]; 
     buffer = postdata.getBytes("UTF8"); 

     bout.write(buffer); 
     byte[] b = bout.toByteArray(); 
     httpConn.setRequestProperty("Content-Length", 
       String.valueOf(b.length)); 

     httpConn.setRequestProperty("Content-Type", "application/json"); 
     httpConn.setRequestMethod("POST"); 

     httpConn.setDoOutput(true); 
     httpConn.setDoInput(true); 

     OutputStream out = httpConn.getOutputStream(); 
     out.write(b); 
     out.close(); 

     InputStreamReader isr = new InputStreamReader(
       httpConn.getInputStream()); 
     BufferedReader in = new BufferedReader(isr); 

     while ((responseString = in.readLine()) != null) { 
      outputString = outputString + responseString; 
     } 
     System.out.println(outputString); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
} 

}

你能幫助我嗎?

回答

0

HTTP 401表示未經授權的訪問。在你的代碼中,即使你創建了Authenticator,你也沒有提供它作爲post請求頭的一部分。這裏是關於如何爲URLConnection設置authenticator的教程。

+0

謝謝你的回覆。但是「Authenticator.setDefault(ma)」這行代碼如何呢? 。我認爲這需要處理所有事情。否則,我應該如何將Authenticator作爲標題的一部分? – Ivelius 2012-01-29 13:17:46

+0

我不認爲setDefaul就夠了。在我的回答中發佈的鏈接中有如何將Authenticator設置爲標題的一部分。 – kosa 2012-01-29 15:54:16

+0

好吧,我按照指南,現在我得到400錯誤。這是我的新代碼和錯誤說明,下面http://pastie.org/3281240 – Ivelius 2012-01-30 09:47:23