我按照下面的教程.
https://developers.google.com/eclipse/docs/running_and_debugging_2_0
它基本上为我现有的应用程序添加了GAE后端.然后,我尝试下面的示例,在本地开发服务器上运行它,然后我得到下面发生的异常
Note result = endpoint.insertNote(note).execute();
叫做.
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
我的代码如下.
package com.cloudnotes;
import java.io.IOException;
import java.util.Date;
import android.os.AsyncTask;
import android.content.Context;
import com.cloudnotes.noteendpoint.Noteendpoint;
import com.cloudnotes.noteendpoint.model.Note;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestinitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new EndpointsTask().execute(getApplicationContext());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main,menu);
return true;
}
public class EndpointsTask extends AsyncTask<Context,Integer,Long> {
protected Long doInBackground(Context... contexts) {
Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
AndroidHttp.newCompatibleTransport(),new JacksonFactory(),new HttpRequestinitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
Note note = new Note().setDescription("Note Description");
String noteID = new Date().toString();
note.setId(noteID);
note.setEmailAddress("E-Mail Address");
Note result = endpoint.insertNote(note).execute();
} catch (IOException e) {
e.printstacktrace();
}
return (long) 0;
}
}
}
解决方法
导致此问题的另一个可能原因是未在appengine-web.xml中设置正确的applicationId:
<?xml version="1.0" encoding="utf-8"?> <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>APPLICATION_ID_HERE</application>
…
</appengine-web-app>