Urban Airship Server Api-java Client Instance __java

Source: Internet
Author: User
Tags auth stringbuffer

This article link: http://blog.csdn.net/kongxx/article/details/8160493

Today, a third-party open Source library http://bitbucket.org/sullis/urbanairship-java/was recommended on the Api,urban airship website at the server end of Urban airship. But after a simple test, some problems were found, and the library didn't seem to be very active. To see how the server-side API for urban airship is used, try to write a small program yourself.

Today's test involves sending a message to the Android device from urban airship, so it is assumed that an Android device or emulator has installed a native app that can receive messages, as a reference to my previous blog post (http:// blog.csdn.net/kongxx/article/details/8155916).

Let's start today's test.

1. Create a project first, or use MAVEN to create a project

MVN archetype:generate-dgroupid=urbanairship.server-dartifactid=urbanairship-server-darchetypeartifactid= Maven-archetype-quickstart-dinteractivemode=false
2. Modify the Pom.xml document as follows

<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>ht tpclient</artifactid> <version>4.2.1</version> </dependency> <dependency> <grou Pid>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> <targ  et>1.6</target> </configuration> </plugin> </plugins> </build> </project>
3. Create a test class, take care to replace the username, password, and 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");
			@Override Public Header getContentType () {Header h = new Basicheader ("Content-type", "Application/json");
		return h; }

	}
}
4. Test, run the simulator first, then run the Android native app that can receive messages, and run the test class above, and wait a moment to see the message on the device or emulator.







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.