7

GCMBroadcast मैं अपने Android एप्लिकेशन में GCM (गूगल मेघ संदेश) को शामिल करने की कोशिश कर रहा हूँ। इसके लिए, मैं this tutorial का पालन कर रहा हूं। नीचे दी गई टिप्पणियां सफलता की रिपोर्ट करती हैं, इसलिए निश्चित रूप से समस्या मेरी तरफ है और ट्यूटोरियल नहीं है।java.lang.IllegalArgumentException: सर्वलेट मानचित्रण निर्दिष्ट करता है एक अज्ञात सर्वलेट नाम

Info- मैं जावा ईई के लिए नवीनतम ग्रहण चल रहा हूँ बिलाव संस्करण 8 (नवीनतम)

यह कंसोल त्रुटि बिल्ला सर्वर चलाने का प्रयास कर रहा है:

Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name GCMBroadcast 
     at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3071) 
     at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3050) 
     at org.apache.catalina.startup.ContextConfig.configureContext(ContextConfig.java:1372) 
     at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1176) 
     at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771) 
     at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305) 
     at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) 
     at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) 
     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5066) 
     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
     ... 6 more 

यहाँ है मेरी प्रोजेक्ट पदानुक्रम बिल्कुल ठीक है कि इसे कैसे होना चाहिए।

मेरे web.xml कोड:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" 
    version="3.0"> 
    <servlet> 
     <servlet-name>com.avilyne.gcm.GCMBroadcast</servlet-name> 
     <servlet-class>com.avilyne.gcm.GCMBroadcast</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>GCMBroadcast</servlet-name> 
     <url-pattern>/gcmbroadcast</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

</web-app> 

मेरे index.jsp फ़ाइल:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<% 
    // retrieve our passed CollapseKey and Message parameters, if they exist. 
    String collapseKey = "GCM_Message"; 
    String message = "Generic Broadcast Message"; 

    Object collapseKeyObj = request.getAttribute("CollapseKey"); 

    if (collapseKeyObj != null) { 
     collapseKey = collapseKeyObj.toString(); 
    } 

    Object msgObj = request.getAttribute("Message"); 

    if (msgObj != null) { 
     message = msgObj.toString(); 
    } 

%> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Android GCM Broadcast</title> 

</head> 
<body> 

    <h2>GCM Broadcast Demo</h2> 

    <form action="GCMBroadcast" method="post"> 
     <label>Broadcast Message </label> 
     <br /><input type="text" name="CollapseKey" value="<%=collapseKey %>" /> 
     <br/><textarea name="Message" rows="3" cols="60" ><%=message %> </textarea> 
     <br/><input type="submit" name="submit" value="Submit" /> 
     </form> 
    </body> 
</html> 

मेरे GCMBroadcast.java फ़ाइल (जो मुझे लगता है कि साझा करने के लिए आवश्यक नहीं है, लेकिन किसी भी लेने नहीं कर रहा हूँ संभावना):

package com.avilyne.gcm; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.*; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import com.google.android.gcm.server.Message; 
import com.google.android.gcm.server.MulticastResult; 
import com.google.android.gcm.server.Sender; 

/** 
* Servlet implementation class GCMBroadcast 
*/ 
@WebServlet("/GCMBroadcast") 
public class GCMBroadcast extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    // The SENDER_ID here is the "Browser Key" that was generated when I 
    // created the API keys for my Google APIs project. 
    private static final String SENDER_ID = "AIzaSyCWryYfPDw3Ge7oMvI2vSeRKoFvsc7yasd"; 

    // This is a *cheat* It is a hard-coded registration ID from an Android device 
    // that registered itself with GCM using the same project id shown above. 
    private static final String DROID_BIONIC = "APA91bEju-eB74DWRChlVt5gh7YfIVzNOr8gRYPisFbmcwBPlMJeGTYmdF7cYR3oL-F9KqmTey016drxmWAkYa4WQv9pQ_KvRzI1VUkql6ObbYGPkV7UBsm6pYoBw0dEk3veh60v3lVhDtLztWIbDc3XqtjU_fE_0g"; 

    // This array will hold all the registration ids used to broadcast a message. 
    // for this demo, it will only have the DROID_BIONIC id that was captured 
    // when we ran the Android client app through Eclipse. 
    private List<String> androidTargets = new ArrayList<String>(); 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public GCMBroadcast() { 

     super(); 

     // we'll only add the hard-coded *cheat* target device registration id 
     // for this demo. 
     androidTargets.add(DROID_BIONIC); 

    } 

    // This doPost() method is called from the form in our index.jsp file. 
    // It will broadcast the passed "Message" value. 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     // We'll collect the "CollapseKey" and "Message" values from our JSP page 
     String collapseKey = ""; 
     String userMessage = ""; 

     try { 
      userMessage = request.getParameter("Message"); 
      collapseKey = request.getParameter("CollapseKey"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return; 
     } 

     // Instance of com.android.gcm.server.Sender, that does the 
     // transmission of a Message to the Google Cloud Messaging service. 
     Sender sender = new Sender(SENDER_ID); 

     // This Message object will hold the data that is being transmitted 
     // to the Android client devices. For this demo, it is a simple text 
     // string, but could certainly be a JSON object. 
     Message message = new Message.Builder() 

     // If multiple messages are sent using the same .collapseKey() 
     // the android target device, if it was offline during earlier message 
     // transmissions, will only receive the latest message for that key when 
     // it goes back on-line. 
     .collapseKey(collapseKey) 
     .timeToLive(30) 
     .delayWhileIdle(true) 
     .addData("message", userMessage) 
     .build(); 

     try { 
      // use this for multicast messages. The second parameter 
      // of sender.send() will need to be an array of register ids. 
      MulticastResult result = sender.send(message, androidTargets, 1); 

      if (result.getResults() != null) { 
       int canonicalRegId = result.getCanonicalIds(); 
       if (canonicalRegId != 0) { 

       } 
      } else { 
       int error = result.getFailure(); 
       System.out.println("Broadcast failure: " + error); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // We'll pass the CollapseKey and Message values back to index.jsp, only so 
     // we can display it in our form again. 
     request.setAttribute("CollapseKey", collapseKey); 
     request.setAttribute("Message", userMessage); 

     request.getRequestDispatcher("index.jsp").forward(request, response); 

    } 

} 

ट्यूटोरियल पुरुष बिलाव 7 उपयोग कर रहा है, जबकि मैं 8. उपयोग कर रहा हूँ मुझे नहीं लगता कि यह है कि एक जनसंपर्क होना चाहिए में oblem। मुझे बस इतना अनुमान लगाने की ज़रूरत है। इस समस्या को छंटनी वास्तव में वास्तव में उपयोगी होगी। मैं thankyou अपना समय :)

+0

फिक्स्ड! @ जेएफपीकार्ड के लिए धन्यवाद। –

उत्तर

4

आपका सर्वलेट नाम मिलान नहीं कर रहे हैं के लिए उन्नत में सभी। सर्वलेट और सर्वलेट-मैपिंग टैग में सर्वलेट-नाम समान होना चाहिए। त्रुटि संदेश स्पष्ट है:

की वजह से: java.lang.IllegalArgumentException: सर्वलेट मानचित्रण निर्दिष्ट करता है एक अज्ञात सर्वलेट नाम

GCMBroadcast

प्रयास करें:

<servlet> 
     <servlet-name>GCMBroadcast</servlet-name> 
     <servlet-class>com.avilyne.gcm.GCMBroadcast</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>GCMBroadcast</servlet-name> 
     <url-pattern>/gcmbroadcast</url-pattern> 
    </servlet-mapping> 
+0

ओएमजी हाँ, धन्यवाद! मुझे एक गाइड के बाद पहला सर्वर नाम बदलना पड़ा, जिसमें कहा गया कि हमें पूरे पैकेज नाम को शामिल करने की आवश्यकता है, लेकिन मुझे नहीं पता था कि मुझे इसे दूसरे नोड पर भी बदलना था! –

+0

क्या आप अभी भी मेरी मदद कर सकते हैं? सर्वर ऊपर और चल रहा है, मैं http: // localhost: 8080/GCMService/link पर जा सकता हूं (लेकिन http: // localhost: 8080 पर जाने का प्रयास करने पर यह मुझे 404 त्रुटि देता है)। फिर अगर मैं सबमिट करने के लिए सबमिट संदेश पर क्लिक करता हूं, तो यह मुझे 404 त्रुटि देता है। मैं क्या कर सकता हूँ? दोनों त्रुटियां केवल ब्राउज़र में दिखाई देती हैं। –

+0

शायद एक और प्रश्न पोस्ट करें – JFPicard

संबंधित मुद्दे