question

skyzoneentertainment avatar image
skyzoneentertainment asked

web server construction

Hi, I am a beginner programmer. So, I don't know much about the server. These url, https://developer.amazon.com/sdk/adm/token.html https://developer.amazon.com/sdk/adm/sending-message.html => I understand the concept. but, I don't know how to build the my server with the 'source code in the above url. I made a json.html (used my web server). the "public String getAuthToken(String clientId, String clientSecret) throws Exception ~~" is used like this form , I am not sure this code is correct. Could you give me a full sample code(how to apply the source code that is in the above url to html or my web server) about requesting an Access Token and Sending a Message?
amazon device messaging
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Skyzoneentertainment, Thank you for posting. You need to setup a web server (preferably Tomcat). Below is ADMAdmin servlet you need to compile with required dependencies then deploy the class in Tomcat. By default, a servlet application is located at the path /webapps/ROOT and the class file would reside in /webapps/ROOT/WEB-INF/classes. Create following entries in web.xml file located in /webapps/ROOT/WEB-INF/ ADMAdmin ADMAdmin ADMAdmin /ADMAdmin Above entries to be created inside ... tags available in web.xml file. There could be various entries in this table already available, but never mind. You are almost done, now let us start tomcat server using \bin\startup.bat (on windows) or /bin/startup.sh (on Linux/Solaris etc.) and finally type below urls in browser's address box. 1. Issue auth token : http://localhost:8080/ADMAdmin?action=getAuthToken 2. Send message to a registered device : http://localhost:8080/ADMAdmin?action=sendMessage&message=TestAdmMessageSentFromTomcatServer&regId=xxxx&token=AUTH_TOKEN_YOU_GOT_BACK_FROM_ADMAdmin Below is the the ADMAdmin servlet class for reference. Dependencies paths: 1. java-json.jar ( http://www.java2s.com/Code/JarDownload/java/java-json.jar.zip) 2. servlet.jar ( http://www.java2s.com/Code/JarDownload/servlet/servlet.jar.zip) ADMAdmin.java ------------------------------ import java.io.*; import java.net.URL; import java.net.URLEncoder; import javax.net.ssl.HttpsURLConnection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; public class ADMAdmin extends HttpServlet { private String CLIENT_ID = "PUT_YOUR_CLIENT_ID"; private String CLIENT_SECRET = "PUT_YOUR_CLIENT_SECRET"; public void init() throws ServletException { // Do required initialization } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if(action.equals("getAuthToken")){ String authToken = ""; try { authToken = getAuthToken(CLIENT_ID, CLIENT_SECRET); } catch (Exception e) { e.printStackTrace(); } response.getWriter().write(authToken); } else if(action.equals("sendMessage")){ String message = request.getParameter("message"); String token = request.getParameter("token"); String regId = request.getParameter("regId"); String resp = "Success"; try { sendMessageToDevice(regId,token,message); } catch (Exception e) { e.printStackTrace(); resp = "fail"; } response.getWriter().write(resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } /** * To obtain an access token, make an HTTPS request to Amazon * and include your client_id and client_secret values. */ public String getAuthToken(String clientId, String clientSecret) throws Exception { // Encode the body of your request, including your clientID and clientSecret values. String body = "grant_type=" + URLEncoder.encode("client_credentials", "UTF-8") + "&" + "scope=" + URLEncoder.encode("messaging:push", "UTF-8") + "&" + "client_id=" + URLEncoder.encode(clientId, "UTF-8") + "&" + "client_secret=" + URLEncoder.encode(clientSecret, "UTF-8"); // Create a new URL object with the base URL for the access token request. URL authUrl = new URL(" https://api.amazon.com/auth/O2/token"); // Generate the HTTPS connection. You cannot make a connection over HTTP. HttpsURLConnection con = (HttpsURLConnection) authUrl.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); // Set the Content-Type header. con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Charset", "UTF-8"); // Send the encoded parameters on the connection. OutputStream os = con.getOutputStream(); os.write(body.getBytes("UTF-8")); os.flush(); con.connect(); // Convert the response into a String object. String responseContent = parseResponse(con.getInputStream()); // Create a new JSONObject to hold the access token and extract // the token from the response. org.json.JSONObject parsedObject = new org.json.JSONObject(responseContent); return parsedObject.getString("access_token"); } private String parseResponse(InputStream in) throws Exception { InputStreamReader inputStream = new InputStreamReader(in, "UTF-8"); BufferedReader buff = new BufferedReader(inputStream); StringBuilder sb = new StringBuilder(); String line = buff.readLine(); while (line != null) { sb.append(line); line = buff.readLine(); } return sb.toString(); } /** * Request that ADM deliver your message to a specific instance of your app. */ public void sendMessageToDevice(String regId, String registrationId, String accessToken) throws Exception { // JSON payload representation of the message. JSONObject payload = new JSONObject(); // Define the key/value pairs for your message content and add them to the // message payload. JSONObject data = new JSONObject(); data.put("firstKey", "firstValue"); data.put("secondKey", "secondValue"); payload.put("data", data); // Add a consolidation key. If multiple messages are pending delivery for a particular // app instance with the same consolidation key, ADM will attempt to delivery the most // recently added item. payload.put("consolidationKey", "ADM_Enqueue_Sample"); // Add an expires-after value to the message of 1 day. If the targeted app instance does not // come online within the expires-after window, the message will not be delivered. payload.put("expiresAfter", 86400); // Convert the message from a JSON object to a string. String payloadString = payload.toString(); // Establish the base URL, including the section to be replaced by the registration // ID for the desired app instance. Because we are using String.format to create // the URL, the %1$s characters specify the section to be replaced. String admUrlTemplate = " https://api.amazon.com/messaging/registrations/%1$s/messages"; URL admUrl = new URL(String.format(admUrlTemplate,registrationId)); // Generate the HTTPS connection for the POST request. You cannot make a connection // over HTTP. HttpsURLConnection conn = (HttpsURLConnection) admUrl.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); // Set the content type and accept headers. conn.setRequestProperty("content-type", "application/json"); conn.setRequestProperty("accept", "application/json"); conn.setRequestProperty("X-Amzn-Type-Version ", "com.amazon.device.messaging.ADMMessage@1.0"); conn.setRequestProperty("X-Amzn-Accept-Type", "com.amazon.device.messaging.ADMSendResult@1.0"); // Add the authorization token as a header. conn.setRequestProperty("Authorization", "Bearer " + accessToken); // Obtain the output stream for the connection and write the message payload to it. OutputStream os = conn.getOutputStream(); os.write(payloadString.getBytes(), 0, payloadString.getBytes().length); os.flush(); conn.connect(); // Obtain the response code from the connection. int responseCode = conn.getResponseCode(); // Check if we received a failure response, and if so, get the reason for the failure. if( responseCode != 200) { if( responseCode == 401 ) { // If a 401 response code was received, the access token has expired. The token should be refreshed // and this request may be retried. } String errorContent = parseResponse(conn.getErrorStream()); throw new RuntimeException(String.format("ERROR: The enqueue request failed with a " + "%d response code, with the following message: %s", responseCode, errorContent)); } else { // The request was successful. The response contains the canonical Registration ID for the specific instance of your // app, which may be different that the one used for the request. String responseContent = parseResponse(conn.getInputStream()); JSONObject parsedObject = new JSONObject(responseContent); String canonicalRegistrationId = parsedObject.getString("registrationID"); // Check if the two Registration IDs are different. if(!canonicalRegistrationId.equals(registrationId)) { // At this point the data structure that stores the Registration ID values should be updated // with the correct Registration ID for this particular app instance. } } } } ---------------------------------- This class is not tested. I just assembled the code together to demonstrate the stuffs to you. Hope it helps. Please get back in case of any issue. Thanks.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

skyzoneentertainment avatar image
skyzoneentertainment answered
Thank you very much. But still I have errors. ㅜㅜ like this, http://98.109.64.40/website/ADMAdmin:8080/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhYk9YMjJjR0xDR3NCMDFWcSs4MVMwbTlON1RhOHRWUEdnWWY0MXF5eWlTSHdIS0F2bXhrRnl0UVFFRm55bDVSYW9HOS96N2thc3NOTGNNcHNuUDJEZk1OdDgzZnZxWW55d1dBUGFtbGtkTU5xN1pnWlM1a1dHZGg4SlpoOTFOZXdmbUVBbjN4QmdyVlJwZjNCck1Od2hxdE5yRnlhWTlyTE1iQ2ZIeU9taVM4aFNUUk1EcHE2cEFBaHB0a3R6bXd1TGpWOE9ZTWN1bk1UTGJuejE1Tm9LTUhub0FubXkvbS83UUI3WEZJbGdZL0N5amFxRHpTQWdKWWpzOXN0eThWc0tmTUsvK3NHaWZTZ2tVTVdWVWJBUzd6d1NpUHV1Um9hRk1Yd05kc1lnL0U9IUM3MmtWbm5oZmQ4UHpONkpEVU9DREE9PQ java.io.FileNotFoundException: http://98.109.64.40/website/ADMAdmin:8080/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhYk9YMjJjR0xDR3NCMDFWcSs4MVMwbTlON1RhOHRWUEdnWWY0MXF5eWlTSHdIS0F2bXhrRnl0UVFFRm55bDVSYW9HOS96N2thc3NOTGNNcHNuUDJEZk1OdDgzZnZxWW55d1dBUGFtbGtkTU5xN1pnWlM1a1dHZGg4SlpoOTFOZXdmbUVBbjN4QmdyVlJwZjNCck1Od2hxdE5yRnlhWTlyTE1iQ2ZIeU9taVM4aFNUUk1EcHE2cEFBaHB0a3R6bXd1TGpWOE9ZTWN1bk1UTGJuejE1Tm9LTUhub0FubXkvbS83UUI3WEZJbGdZL0N5amFxRHpTQWdKWWpzOXN0eThWc0tmTUsvK3NHaWZTZ2tVTVdWVWJBUzd6d1NpUHV1Um9hRk1Yd05kc1lnL0U9IUM3MmtWbm5oZmQ4UHpONkpEVU9DREE9PQ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) at com.amazon.sample.admmessenger.MyServerMsgHandler$1.doInBackground(MyServerMsgHandler.java:63) I did as you taught. and but It's not working ... At first, 1. Issue auth token : http://localhost:8080/ADMAdmin?action=getAuthToken 2. Send message to a registered device : http://localhost:8080/ADMAdmin?action=sendMessage&message=TestAdmMessageSentFromTomcatServer&regId=xxxx&token=AUTH_TOKEN_YOU_GOT_BACK_FROM_ADMAdmin _________________>>>>> this is not work.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

skyzoneentertainment avatar image
skyzoneentertainment answered
I am sorry , I didn't explain my error concretely when i do this, http://localhost:8080/ADMAdmin?action=getAuthToken there are errors, exception javax.servlet.ServletException: Servlet execution threw an exception root cause java.lang.NoClassDefFoundError: org/json/JSONObject website.web.ADMAdmin.getAuthToken(ADMAdmin.java:114) website.web.ADMAdmin.doGet(ADMAdmin.java:49) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) ==> I think this error is about .jar . I used the jar that you taught me. There are servlet-api.jar, jsp-api.jar, and [java-json.jar, servlet.jar (you gave me)] in ther Referenced Libraries of My project.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Skyzoneentertainment, Can you check WEB-INF/lib folder whether the json library is there or not? I think you dont have to put sevlet jar there as that should already be available in the tomacat's run time. I would recommend you to go through below link for better clarity http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html#Class_Loader_Definitions
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

skyzoneentertainment avatar image
skyzoneentertainment answered
thank you.^^ I resolve this problem about the servlet.jar. !!!!!!!!!!!! I have problem another. i used the tomcat Version 7.!!!!!!!!!!!! I have 3 issues. Thanks to you, I can get a access token. and send the message. !!!!!( but , i can not receive the response message.) I used the source code that you gave me. The source code is in this address ( https://forums.developer.amazon.com/forums/thread.jspa?threadID=1452&tstart=0) [ the number of the parameters is incorrect, In "public void sendMessageToDevice(String regId, String registrationId, String accessToken) throws Exception" there are three parameters, but we just need 2 parameters] 1. First Question. I know the message is { firstkey firstValue secondKey secondValue ..}. because this, JSONObject data = new JSONObject(); data.put("firstKey", "firstValue"); data.put("secondKey", "secondValue"); payload.put("data", data); => Is right? If It is not correct, how to send the message that i want. I got the Registeration ID when i run the sample ADMMessenger project through Logcat. so, when i used the source code(in this address ( https://forums.developer.amazon.com/forums/thread.jspa?threadID=1452&tstart=0)) I input the registerationId directly.!!! ============================================================ anyway, I succeeded in sending the message But, I can not receive the message. 2. Second Question (why this error is occured?) when i run the sample ADMMessenger, there are error. like this, http://98.109.64.40/ADMAdmin:8080/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhYk9YMjJjR0xDR3NCMDFWcSs4MVMwbTlON1RhOHRWUEdnWWY0MXF5eWlTSHdIS0F2bXhrRnl0UVFFRm55bDVSYW9HOS96N2thc3NOTGNNcHNuUDJEZk1OdDgzZnZxWW55d1dBUGFtbGtkTU5xN1pnWlM1a1dHZGg4SlpoOTFOZXdmbUVBbjN4QmdyVlJwZjNCck1Od2hxdE5yRnlhWTlyTE1iQ2ZIeU9taVM4aFNUUk1EcHE2cEFBaHB0a3R6bXd1TGpWOE9ZTWN1bk1UTGJuejE1Tm9LTUhub0FubXkvbS83UUI3WEZJbGdZL0N5amFxRHpTQWdKWWpzOXN0eThWc0tmTUsvK3NHaWZTZ2tVTVdWVWJBUzd6d1NpUHV1Um9hRk1Yd05kc1lnL0U9IUM3MmtWbm5oZmQ4UHpONkpEVU9DREE9PQ java.io.FileNotFoundException: http://98.109.64.40/ADMAdmin:8080/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhYk9YMjJjR0xDR3NCMDFWcSs4MVMwbTlON1RhOHRWUEdnWWY0MXF5eWlTSHdIS0F2bXhrRnl0UVFFRm55bDVSYW9HOS96N2thc3NOTGNNcHNuUDJEZk1OdDgzZnZxWW55d1dBUGFtbGtkTU5xN1pnWlM1a1dHZGg4SlpoOTFOZXdmbUVBbjN4QmdyVlJwZjNCck1Od2hxdE5yRnlhWTlyTE1iQ2ZIeU9taVM4aFNUUk1EcHE2cEFBaHB0a3R6bXd1TGpWOE9ZTWN1bk1UTGJuejE1Tm9LTUhub0FubXkvbS83UUI3WEZJbGdZL0N5amFxRHpTQWdKWWpzOXN0eThWc0tmTUsvK3NHaWZTZ2tVTVdWVWJBUzd6d1NpUHV1Um9hRk1Yd05kc1lnL0U9IUM3MmtWbm5oZmQ4UHpONkpEVU9DREE9PQ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) at com.amazon.sample.admmessenger.MyServerMsgHandler$1.doInBackground(MyServerMsgHandler.java:63) at com.amazon.sample.admmessenger.MyServerMsgHandler$1.doInBackground(MyServerMsgHandler.java:1) ...... ======================================================================================= 3.Third Question(the serveraddress (local) ) Actually I am not sure the server_address and server_port in the strings.xml of the sample ADMMessenger projects. I input , http://98.109.64.40/ADMAdmin // this is my local address. when a outside person try to access 8080 Is right? if not, Should i use another address?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

skyzoneentertainment avatar image
skyzoneentertainment answered
Thanks .
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

skyzoneentertainment avatar image
skyzoneentertainment answered
Thanks to you, I improve the sample app(ADMMessenger)
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi skyzoneentertainment, I would recommend you to continue in a single thread. You multiple posts are creating confusion for me what the issue is. Any way, I think all the three problems are originated due to wrong url formation from your android client. I can see the url you are generating is like http://98.109.64.40/ADMAdmin:8080/register?device=amzn1.adm-...... It should be http://98.109.64.40:8080/ADMAdmin/register?device=amzn1.adm-... So the port no should be after the IP separated with a ":" You string params should look like this: 98.109.64.40 8080 And in the ADMMessenger client, MyServerMsgHandler.registerAppInstance() should have this, String URL = context.getString(R.string.server_address) + ":" + context.getString(R.string.server_port); String fullUrl = URL + "/ADMAdmin/register?device="+ registrationId;
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gamingtilidrop avatar image
Gamingtilidrop answered
I really thank you. ^^!!!!! I would like to test in local environment. INPUT: 127.0.0.1 8080 RESULT: ERROR failed to connect to /127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused) java.net.ConnectException: failed to connect to /127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused) at libcore.io.IoBridge.connect(IoBridge.java:114) ... INPUT http://localhost 8080 RESULT: ERROR failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused) java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused) at libcore.io.IoBridge.connect(IoBridge.java:114) ..... INPUT: http://192.1xx.x.x // I got this ip address using instruction "ipconfig" through cmd. 8080 RESULT: ERROR http://192.168.1.2:8080/ADMAdmin/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhc0NWSFNhWWpVMmxOL1dqd3I2UG9ralRWWXNDT0RMampDMGpHcm13YVQvRTZlc2wvcXlJZUVUQnZIQ2dWQWZvUkp4TWZnU2ErK2NNL1RNdUtHZzNUdWtvSEtiS0tuekYrMk5NdzdXVXN3QVNGSkVQRjVDQk1SMnZGbEw4emFORUZLUlVWTzhYQ3VwSnN4VTU0UXJKaHIrMUZUSDQ2TnpGRFk4eU5RT0RyV1J4ZTNVczhKai9EMC9lSStNTzU4Q3pMeHhLZEppTlVhdGRVcUM1VjFueUpTaGtSRCtxb3lMSkVReW5DL2VQc05xSjh6MitkbWFnWkFJT1JsUmNlVE5PNGs0VUFoY2NtT0tlRkd6SFJtZlJJS1IxQzBuMkdGZmZjNW5lWEd1SHAvTDA9IUFtS3lYaEVuM3RGeGp1RllsT0MyMkE9PQ java.io.FileNotFoundException: http://192.168.1.2:8080/ADMAdmin/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhc0NWSFNhWWpVMmxOL1dqd3I2UG9ralRWWXNDT0RMampDMGpHcm13YVQvRTZlc2wvcXlJZUVUQnZIQ2dWQWZvUkp4TWZnU2ErK2NNL1RNdUtHZzNUdWtvSEtiS0tuekYrMk5NdzdXVXN3QVNGSkVQRjVDQk1SMnZGbEw4emFORUZLUlVWTzhYQ3VwSnN4VTU0UXJKaHIrMUZUSDQ2TnpGRFk4eU5RT0RyV1J4ZTNVczhKai9EMC9lSStNTzU4Q3pMeHhLZEppTlVhdGRVcUM1VjFueUpTaGtSRCtxb3lMSkVReW5DL2VQc05xSjh6MitkbWFnWkFJT1JsUmNlVE5PNGs0VUFoY2NtT0tlRkd6SFJtZlJJS1IxQzBuMkdGZmZjNW5lWEd1SHAvTDA9IUFtS3lYaEVuM3RGeGp1RllsT0MyMkE9PQ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) ...... ================================================== Q1 *****( i think the input is correct, but I got an error) INPUT: 192.1xx.x.x // I got this ip address using instruction "ipconfig" through cmd. 8080 RESULT: ERROR Protocol not found: 192.168.1.2:8080/ADMAdmin/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhc0NWSFNhWWpVMmxOL1dqd3I2UG9ralRWWXNDT0RMampDMGpHcm13YVQvRTZlc2wvcXlJZUVUQnZIQ2dWQWZvUkp4TWZnU2ErK2NNL1RNdUtHZzNUdWtvSEtiS0tuekYrMk5NdzdXVXN3QVNGSkVQRjVDQk1SMnZGbEw4emFORUZLUlVWTzhYQ3VwSnN4VTU0UXJKaHIrMUZUSDQ2TnpGRFk4eU5RT0RyV1J4ZTNVczhKai9EMC9lSStNTzU4Q3pMeHhLZEppTlVhdGRVcUM1VjFueUpTaGtSRCtxb3lMSkVReW5DL2VQc05xSjh6MitkbWFnWkFJT1JsUmNlVE5PNGs0VUFoY2NtT0tlRkd6SFJtZlJJS1IxQzBuMkdGZmZjNW5lWEd1SHAvTDA9IUFtS3lYaEVuM3RGeGp1RllsT0MyMkE9PQ java.net.MalformedURLException: Protocol not found: 192.168.1.2:8080/ADMAdmin/register?device=amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhc0NWSFNhWWpVMmxOL1dqd3I2UG9ralRWWXNDT0RMampDMGpHcm13YVQvRTZlc2wvcXlJZUVUQnZIQ2dWQWZvUkp4TWZnU2ErK2NNL1RNdUtHZzNUdWtvSEtiS0tuekYrMk5NdzdXVXN3QVNGSkVQRjVDQk1SMnZGbEw4emFORUZLUlVWTzhYQ3VwSnN4VTU0UXJKaHIrMUZUSDQ2TnpGRFk4eU5RT0RyV1J4ZTNVczhKai9EMC9lSStNTzU4Q3pMeHhLZEppTlVhdGRVcUM1VjFueUpTaGtSRCtxb3lMSkVReW5DL2VQc05xSjh6MitkbWFnWkFJT1JsUmNlVE5PNGs0VUFoY2NtT0tlRkd6SFJtZlJJS1IxQzBuMkdGZmZjNW5lWEd1SHAvTDA9IUFtS3lYaEVuM3RGeGp1RllsT0MyMkE9PQ at java.net.URL. (URL.java:178) at java.net.URL. (URL.java:127) at com.amazon.sample.admmessenger.MyServerMsgHandler$1.doInBackground(MyServerMsgHandler.java:55) ...... So, I changed the network firewall setting(on control panel. not router). But It's not working. Is the "server_address" above correct? ========================================================================================================================= Q2. I know “MyServerMsgHandler.java” sent ‘Register Id’ to app server.(String fullUrl = URL + "/ADMAdmin/register?device="+ registrationId; ) Should I give action to register using the code below? String action = request.getParameter("action"); if (action.equals("getAuthToken")) {..... ========================================================================================================================= Q3. I don’t know how to receive ‘RegisterId’ by app to send the message. SO, I coded like this, if (action.equals("sendMessage")) { // String message = request.getParameter("message"); String Token = accessToken; String regId = "amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhc0NWSFNhWWpVMmxOL1dqd3I2UG9ralRWWXNDT0RMampDMGpHcm13YVQvRTZlc2wvcXlJZUVUQnZIQ2dWQWZvUkp4TWZnU2ErK2NNL1RNdUtHZzNUdWtvSEtiS0tuekYrMk5NdzdXVXN3QVNGSkVQRjVDQk1SMnZGbEw4emFORUZLUlVWTzhYQ3VwSnN4VTU0UXJKaHIrMUZUSDQ2TnpGRFk4eU5RT0RyV1J4ZTNVczhKai9EMC9lSStNTzU4Q3pMeHhLZEppTlVhdGRVcUM1VjFueUpTaGtSRCtxb3lMSkVReW5DL2VQc05xSjh6MitkbWFnWkFJT1JsUmNlVE5PNGs0VUFoY2NtT0tlRkd6SFJtZlJJS1IxQzBuMkdGZmZjNW5lWEd1SHAvTDA9IUFtS3lYaEVuM3RGeGp1RllsT0MyMkE9PQ"; //String regId= request.getParameter("regId"); String resp = "Success"; …………. public void sendMessageToDevice(String registrationId, String accessToken) throws Exception { JSONObject payload = new JSONObject(); JSONObject data = new JSONObject(); data.put("firstKey", "firstValue"); data.put("secondKey", "secondValue"); ………………………..
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
First make sure that your server is running on 8080 port or not. You can just click on this url to check it : http://127.0.0.1:8080/ It should open up browser and show you a page in browser. Q1. protocol "http" is missing in the url. Q2. You should persist the reg id in the back end while you receive it in the client. regId regId (action=saveRegId) Persist regId in Data base against the user id ADM Client -------> APP Client -----------------------------------> APP Server ----------------------------------------------------------------> data base Then from server you can run a job which will send the message to that particular reg id. In my example, I added a get parameter to run the job by a http get request with action. Please go through the example what I have given in below thread http://forums.developer.amazon.com/forums/thread.jspa?threadID=1452&tstart=0 Q3. Same answer as Q2.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.