Urban Airship Server API - Java用戶端執行個體__Java

來源:互聯網
上載者:User

本文連結:http://blog.csdn.net/kongxx/article/details/8160493

今天在看Urban Airship的server端的API,Urban Airship官方網站上推薦了一個第三方的開源庫http://bitbucket.org/sullis/urbanairship-java/,但是在經過簡單測試後還是發現了一些問題,並且這個庫好像也不是很活躍。為了看看Urban Airship的server端API怎麼用,自己還是決定自己寫個小程式試試看。

今天的測試由於涉及從Urban Airship向Android裝置發送訊息,所以這裡假定已經有Android裝置或者模擬器安裝了可以接收訊息的native app,具體步驟可以參考我的前一篇部落格(http://blog.csdn.net/kongxx/article/details/8155916)。

下面開始今天的測試

1. 首先建立一個工程,這裡還是使用maven來建立工程

mvn archetype:generate -DgroupId=urbanairship.server -DartifactId=urbanairship-server -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2. 修改pom.xml檔案如下

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>urbanairship.server</groupId><artifactId>urbanairship-server</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>urbanairship-server</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.1</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build></project>
3. 建立一個測試類別Test,注意替換其中的username, password和apid。

package urbanairship.server;import java.io.UnsupportedEncodingException;import java.util.logging.Level;import java.util.logging.Logger;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.StatusLine;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicHeader;import org.apache.http.util.EntityUtils;public class Test {private static Logger logger = Logger.getLogger(Test.class.getName());private static final String username = "This should be Application Key";private static final String password = "This should be Application Master Secret";private static final String apid = "This should be your APID of your android device";public static void main(String[] args) throws Exception {StringBuffer sb = new StringBuffer();sb.append("{\"android\": {\"alert\": \"hello world\"}, \"apids\": [\""+apid+"\"]}");String path = "https://go.urbanairship.com/api/push/";DefaultHttpClient httpClient = new DefaultHttpClient();httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));HttpPost httpPost = new HttpPost(path);httpPost.setHeader("Accept", "application/json");httpPost.setEntity(new JsonEntity(sb.toString()));logger.log(Level.INFO, "executing request: " + httpPost.getRequestLine());HttpResponse response = httpClient.execute(httpPost);StatusLine status = response.getStatusLine();int statusCode = status.getStatusCode();logger.log(Level.INFO, ""+statusCode);HttpEntity responseEntity = response.getEntity();logger.log(Level.INFO,  EntityUtils.toString(responseEntity));}static private class JsonEntity extends StringEntity {public JsonEntity(String jsonString) throws UnsupportedEncodingException {super(jsonString, "UTF-8");}@Overridepublic Header getContentType() {Header h = new BasicHeader("Content-Type", "application/json");return h;}}}
4. 測試,首先運行模擬器,然後運行可以接收message的android native app,最後運行上面的測試類別,稍等片刻就會在裝置或者模擬器上看到訊息了。







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.