나는 UA 자습서를 따라 갔고 내 APID를 얻었고 안드로이드 장치에서 테스트 푸시 메시지를 성공적으로 수신했습니다.도시 비행선 푸시 코드
이제 다음으로 JAVA를 사용하여 내 장치를 타겟팅하고 푸시 메시지를 보냅니다. 지금까지 내가 본 것 중에서 가장 좋은 방법은 웹 API를 사용하는 것입니다.
나는 항상 다음과 같은 오류 얻을 후 메시지를 보내려고하지만 경우 :
때 java.io.IOException : URL에 대한 401 : 서버는 HTTP 응답 코드를 반환 https://go.urbanairship.com/api/push/ 에서 sun.net을 .www.protocol.http.HttpURLConnection.getInputStream (알 수없는 소스) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream (알 수없는 소스) 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();
}
}
은}
은 당신이 나를 도와 주실 수 있습니까?
답장을 보내 주셔서 감사합니다. 그러나 "Authenticator.setDefault (ma);"라인은 어떨까요? . 나는 그것이 모든 것을 돌 보았다고 생각했다. 그렇지 않은 경우 인증 프로그램을 헤더의 일부로 어떻게 부착해야합니까? – Ivelius
나는 setDefaul로 충분하다고 생각하지 않는다. 내 대답에 게시 된 링크에서 인증자를 헤더의 일부로 설정하는 방법이 있습니다. – kosa
좋아, 나는 가이드를 따라 지금은 400 오류 있어요. 아래에 새 코드와 오류 설명이 나와 있습니다. http://pastie.org/3281240 – Ivelius