2016-10-11 73 views
1

現在我正嘗試在我的Java程序中實現Firebase雲消息傳遞。通過FCM發送推送通知。現在,FCM告訴我send it like an HTTP POST request如何從Java程序發送HTTP POST請求?

這是請求我想送:

https://fcm.googleapis.com/fcm/send 
Content-Type:application/json 
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA 

{ "data": { 
    "score": "5x1", 
    "time": "15:10" 
    }, 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." 
} 

如何發送這個從我的Eclipse Java程序?

+0

這可能會有所幫助:http://stackoverflow.com/questions/4205980/java-s結束-http-parameters-via-post-method-easily – JitterbugChew

+0

@JitterbugChew我無法準確理解HTTP請求在那個答案中。我應該在那裏寫我的代碼。我應該用我的什麼代碼替換.. – Peter

+0

你已經有'key = AIzaSyZ-1u ... 0GBYzPu7Udno5aA'嗎? –

回答

2

這是很容易與HttpClient的,我這裏是

import java.io.IOException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONObject; 

public class HttpClientUtils { 

    public JSONObject doPost() { 
     HttpClient httpclient = HttpClientBuilder.create().build(); 
     HttpPost request = new HttpPost("https://fcm.googleapis.com/fcm/send"); 
     JSONObject result = new JSONObject(); 
     try { 
      String bodyContent = "{ 'data': { 'score': '5x1',  'time': '15:10' }, 'to' : 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...'}"; 
      StringEntity requestBody = new StringEntity(bodyContent); 
      request.setEntity(requestBody); 
      request.setHeader("Content-Type", "application/json"); 
      request.setHeader("Authorization", "key=AIzaSyZ-1u...0GBYzPu7Udno5aA"); 
      HttpResponse response = httpclient.execute(request); 
      HttpEntity entity = response.getEntity(); 
      String responseString = EntityUtils.toString(entity); 
      result.put("status", response.getStatusLine().getStatusCode()); 
      result.put("bodyContent", new JSONObject(responseString)); 
     } catch (ClientProtocolException e) { 
      result.put("status", "500"); 
      result.put("bodyContent", ""); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      result.put("status", "500"); 
      result.put("bodyContent", ""); 
      e.printStackTrace(); 
     } finally { 
      request.releaseConnection(); 
     } 
     return result; 
    } 


} 
+0

好的,這裏有兩個問題。 1)導入時出現此錯誤消息'無法解析org.apache.http.impl.client.HttpClientBuilder的導入。 2)當試圖'request.releaseConnection()'我得到這個錯誤消息'方法releaseConnection()是未定義的類型HttpPost' .. – Peter

+0

對於缺少的lib,使用此依賴項
https://mvnrepository.com/artifact /org.apache.httpcomponents/httpclient/4.5.2 –

0

創建FCM請求類 -

@JsonInclude(JsonInclude.Include.NON_NULL) 
    public static class FCMRequest { 
     @JsonProperty("type") 
     private String type; 
     @JsonProperty("expiry") 
     private String expiry; 
     @JsonProperty("body") 
     private String body; 
     @JsonProperty("title") 
     private String title; 
     @JsonProperty("subscriberId") 
     private String subscriberId; 

     public String getType() { 
      return type; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

     public String getExpiry() { 
      return expiry; 
     } 

     public void setExpiry(String expiry) { 
      this.expiry = expiry; 
     } 

     public String getBody() { 
      return body; 
     } 

     public void setBody(String body) { 
      this.body = body; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public void setTitle(String title) { 
      this.title = title; 
     } 

     public String getSubscriberId() { 
      return subscriberId; 
     } 

     public void setSubscriberId(String subscriberId) { 
      this.subscriberId = subscriberId; 
     } 
    } 

創建FCM請求負載和發送POST請求FCM,您可以用球衣休息(JAX-RS)庫如下 -

FCMRequest fcmRequest = new FCMRequest(); 
//set the rquest data 
    WebTarget target = client.target("https://fcm.googleapis.com/fcm/send"); 
       Entity<FCMRequest> entity = Entity.entity(fcmRequest, MediaType.APPLICATION_JSON_TYPE); 
       Response authResponse = target.request() 
         .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) 
         .header(HttpHeaders.AUTHORIZATION, KEY) 
         .post(entity, Response.class); 
+0

當我創建一個新類並添加你的內容時,我得到了所有'JsonInclude'和'JsonPropertys'的錯誤(''JsonInclude不能解析爲類型'而JsonProperty不能解析被解析爲類型')。 – Peter

+0

只需確保在類初始化之前導入了以下類: import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; –