2012-01-29 9 views
1

私はUAのチュートリアルに従って、私のAPIDを取得し、アンドロイドデバイスでテストプッシュメッセージを受信しました。Urban Airshipプッシュコード

次に、私がやりたいことは、JAVAを使用してデバイスをターゲットにしてプッシュメッセージを送信することです。 私がこれまでに見たことから、これを達成する最も良い方法は、Web APIを使用することです。

しかし

私は、私はいつも次のエラーGET、POSTメッセージの送信しようとしています:

にjava.io.IOExceptionを:sun.netでhttps://go.urbanairship.com/api/push/ :URLの401:サーバーは、HTTPレスポンスコードを返し.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は不正アクセスを表します。あなたの作成したオーセンティケータがあなたのコードで、あなたはそれを投稿後のヘッダの一部として提供しませんでした。 URLConnectionにauthenticatorを設定する方法のチュートリアルです。

+0

ご返信ありがとうございます。しかし、 "Authenticator.setDefault(ma);"という行はどうですか? 。私はそれがすべてを世話すると思った。そうでない場合オーセンティケータをヘッダーの一部としてどのように添付しますか? – Ivelius

+0

私はsetDefaulで十分だとは思わない。私の答えに投稿したリンクには、オーセンティケータをヘッダの一部として設定する方法があります。 – kosa

+0

さて、私はガイドに従って、今私は400エラーを得ています。以下は、新しいコードとエラーの説明です(http://pastie.org/3281240)。 – Ivelius

関連する問題