2015-01-27 3 views
0

이 명령이 있습니다.JAVA에서 HTTP를 사용하여 헤더를 보내는 방법

# curl --header "Authorization: key=$api_key" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}" 

내 장치에서 푸시 알림을 보내고 있습니다. 지금은 자바를 보내려고하지만 내 코드가 작동하지 않습니다.

String body = "{\"registration_ids\":[\"ABC\"]}"; 
HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send"); 
StringEntity stringentity = new StringEntity(body, "UTF-8"); 

httppost.addHeader("Content-Type","application/json"); 
    httppost.addHeader("Authorization: key", "AIza*********YUI"); 
httppost.setEntity(stringentity); 
HttpClient httpclient = new DefaultHttpClient(); 
    HttpResponse response; 
    try { 
     response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 

    String strresponse = null; 
    if (entity != null) { 
     strresponse = EntityUtils.toString(entity); 
     System.out.println("strresponse = "+strresponse); 
    } 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

제가 누락 된 부분을 혼동합니다. 이 문서 http://developer.android.com/google/gcm/http.html#request은 본문으로 헤더를 보내야한다고 말했습니다.

httppost.addHeader("Authorization", "key=AIza*********YUI"); 

이 문제를 해결해야한다 : 당신의 curl 예를 바탕으로

+1

무슨 뜻입니까?하지만 내 코드가 아닌 것은 무엇입니까? 예외가 발생 했습니까? 당신이 경험 한 결과가 있습니까? ** 실제로 ** 잘못된 내용을 우리에게 말하지 않으면 어떻게 도와 드릴까요? –

답변

0

이 당신의 Authorization 헤더해야합니다. 방금 참조 된 documentation으로이 헤더를 확인했습니다.

+0

지금 일해 주셔서 감사합니다. 잘못된 헤더를 사용하고있었습니다. –

관련 문제