[英]java.io.FileNotFoundException: read.txt (The system cannot find the file specified)`
[英]RegId required: java.io.FileNotFoundException: GCMRegId.txt (The system cannot find the file specified)
我編寫了一個程序,使用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)並刪除未使用的代碼。
您將獲得輸出。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.