2012-01-29 109 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