所需的RegId:java.io.FileNotFoundException:GCMRegId.txt(系统找不到指定的文件)

大山

我编写了一个程序,使用jsp在android中实现Google Cloud Messaging。从这里获得了参考(http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/)当我尝试在Windows中运行程序时,我无法在服务器上运行我的日食给了我GCMRegId.txt丢失的错误。这是代码:

 GCMNotification.java

@WebServlet("/GCMNotification")
public class GCMNotification extends HttpServlet {

private static final String GOOGLE_SERVER_KEY = "AIzaSyDA5dlLInMsasJEUTIHV0u7maB82MCasdU";
static final String MESSAGE_KEY = "message";    

public GCMNotification() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);

}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {


    Result result = null;

    String share = request.getParameter("shareRegId");

    // GCM RedgId of Android device to send push notification
    String regId = "APA91bG6cLX8OYyZDRypuRq6uodZ9PuMfiql9k6K66i9hZFnS5xr-QmT13nRozGQ10KXNmcMqbsLCVUIS9joGiOPasdfghjklp_AmH2iNs1YVqY3IYzgHoQ";
    if (share != null && !share.isEmpty()) {
        regId = request.getParameter("regId");
        PrintWriter writer = new PrintWriter("GCMRegId.txt");
        writer.println(regId);
        writer.close();
        request.setAttribute("pushStatus", "GCM RegId Received.");
        request.getRequestDispatcher("index.jsp")
                .forward(request, response);
    } else {

        try {
            BufferedReader br = new BufferedReader(new FileReader(
                    "GCMRegId.txt"));
            regId = br.readLine();
            br.close();
            String userMessage = request.getParameter("message");
            Sender sender = new Sender(GOOGLE_SERVER_KEY);
            Message message = new Message.Builder().timeToLive(30)
                    .delayWhileIdle(true).addData(MESSAGE_KEY, userMessage).build();
            System.out.println("regId: " + regId);
            result = sender.send(message, regId, 1);
            request.setAttribute("pushStatus", result.toString());
        } catch (IOException ioe) {
            ioe.printStackTrace();
            request.setAttribute("pushStatus",
                    "RegId required: " + ioe.toString());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("pushStatus", e.toString());
        }
        request.getRequestDispatcher("index.jsp")
                .forward(request, response);
    }
}

JSP页面:index.jsp

  <head>
 </head>
 <body>

<h1>Google Cloud Messaging (GCM) Server in Java</h1>
 <%
String pushStatus = "";
Object pushStatusObj = request.getAttribute("pushStatus");

if (pushStatusObj != null) {
    pushStatus = pushStatusObj.toString();
}
 %>
<form action="GCMNotification" method="post">

    <div>
          <textarea rows="2" name="message" cols="23"
          placeholder="Message to transmit via GCM"></textarea>
    </div>
    <div>
        <input type="submit" value="Send Push Notification via GCM" />
    </div>
</form>
<p>
    <h3>
        <%=pushStatus%>
    </h3>
</p>

它总是给我类似所需的RegId的错误:java.io.FileNotFoundException:GCMRegId.txt(系统找不到指定的文件)我该如何解决该错误?

大山

嘿,我解决了这个问题。GCMRegId.txt文件自动生成。不手动制作该文件。

或者,如果您不想制作GCMRegId.txt文件,只需从代码中删除,然后将DeviceId传递给regID(string)并删除未使用的代码。

您将获得输出。谢谢。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.io.FileNotFoundException:text.txt(系统找不到指定的文件)

java.io.FileNotFoundException:系统找不到指定的文件

系统找不到Java中指定的文件

java.io.FileNotFoundException,找不到文件

FileNotFoundException(系统找不到指定的路径)

java.io.IOException:系统找不到写文本文件所指定的路径

java.io.FileNotFoundException:在tomcat中部署文件后,src \ main \ resources \ Report \ testQuery.xlsx(系统找不到指定的路径)

java.io.FileNotFoundException:(只读文件系统)Mac

java io FileNotFoundException

如何摆脱-“ java.io.IOException:系统找不到指定的路径”

WebSphere Application Server 8:错误404:java.io.FileNotFoundException:SRVE0190E:找不到文件:

keytool错误:java.io.FileNotFoundException:C:\ AndroidWorkspace \ androidKey \ public.jks(系统找不到指定的路径)

系统找不到指定的Java文件

从相对路径读取XML-java.io.FileNotFoundException :(系统找不到指定的路径)

System.IO.FileNotFoundException:找不到文件

System.IO.FileNotFoundException:无法加载文件或程序集。系统找不到文件SharpSvn.dll

java.io.FileNotFoundException:(系统找不到指定的路径)

文件错误为:-java.io.FileNotFoundException:\ files \ storetime.txt(系统找不到指定的路径)

java.io.FileNotFoundException:找不到文件configuration.yml

java.io.FileNotFoundException:打开失败:EROFS(只读文件系统)

java.io.FileNotFoundException,系统找不到指定的路径(cvs文件)java

java.io.FileNotFoundException:read.txt(系统找不到指定的文件)

IO 异常 java.io.FileNotFoundException: (文件名太长)

java.io.IOException:无法运行程序“python”:CreateProcess error=2,系统找不到指定的文件

System.IO.FileNotFoundException:找不到文件

java.io.FileNotFoundException json文件找不到

java net.sf.jasperreports.engine.JRRuntimeException: java.io.IOException: 系统找不到指定的路径

java.io.IOException : ipm.note 。该系统找不到指定的文件

Azure (C#) System.IO.FileNotFoundException:找不到文件