[英]Need Help Connecting to Google App Engine With Retrofit
I have successfully followed the Google tutorial here , using the Android Studio Servlets module to connect to Google App Engine. 我已成功使用Android Studio Servlets模块连接到Google App Engine,并在这里成功遵循了Google教程。 I was able to see the Toast message on my device, meaning I successfully connected to the server and received a response.
我能够在设备上看到Toast消息,这意味着我已成功连接到服务器并收到了响应。
I noticed that this module uses AsyncTask to handle the background tasks. 我注意到该模块使用AsyncTask来处理后台任务。 From what I understand, Retrofit is a much simpler and effective method of handling tasks in the background thread.
据我了解,Retrofit是一种在后台线程中处理任务的更为简单有效的方法。 I am basically trying to replicate the Google Tutorial mentioned above using Retrofit 1.9.0 instead of the ServletPostAsyncTask Java class that they provide.
我基本上是在尝试使用Retrofit 1.9.0(而不是他们提供的ServletPostAsyncTask Java类)复制上面提到的Google教程。
Below is my code: 下面是我的代码:
MainActivity: 主要活动:
public class MainActivity extends AppCompatActivity {
//set the URL of the server, as defined in the Google Servlets Module Documentation
private static String PROJECT_URL = "http://retrofit-test-1203.appspot.com/hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Instantiate a new RestAdapter Object, setting the endpoint as the URL of the server
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(PROJECT_URL)
.build();
//Instantiate a new UserService object, and call the "testRequst" method, created in the interface
//to interact with the server
UserService userService = restAdapter.create(UserService.class);
userService.testRequest("Test_Name", new Callback<String>() {
@Override
public void success(String s, Response response) {
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
UserService Interface, required by Retrofit: 改造所需的UserService接口:
public interface UserService {
static String PROJECT_URL = "http://retrofit-test-1203.appspot.com/hello";
@POST(PROJECT_URL)
void testRequest(@Query("test") String test, Callback<String> cb);
}
My Servlet, as required by the Google Servlets Module: Google Servlets模块要求的我的Servlet:
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Please use the form to POST to this url");
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String name = req.getParameter("name");
resp.setContentType("text/plain");
if(name == null) {
resp.getWriter().println("Please enter a name");
}
resp.getWriter().println("Hello " + name);
}
}
In my userService.testRequest()method, I pass in "Test_Name" as the string parameter. 在我的userService.testRequest()方法中,我传入“ Test_Name”作为字符串参数。 This text is what I hope to pass to the server, and then see a toast that displays "Hello Test_Name" (after receiving a server response), just like the Google App Engine Servlets module explains.
这段文字是我希望传递给服务器的内容,然后看到显示“ Hello Test_Name”的祝酒词(收到服务器响应后),就像Google App Engine Servlets模块说明的那样。
Right now, I am receiving the below error: 现在,我收到以下错误:
Any advice on using Retrofit with Google App Engine is appreciated, as there is limited documentation. 由于文档数量有限,因此不建议将Retrofit与Google App Engine一起使用。
First of all, your base url should just be the domain of the site ie http://retrofit-test-1203.appspot.com (it should not include the path to the resource you're trying to access) So declare it like this: 首先,您的基本url应该只是站点的域,即http://retrofit-test-1203.appspot.com (它不应包含您要访问的资源的路径),因此请声明为这个:
private static String PROJECT_URL = "http://retrofit-test-1203.appspot.com"
Second, don't use the base url(PROJECT_URL in this case) as the relative URL in the declaration of the UserService interface. 其次,不要在UserService接口的声明中使用基本url(在这种情况下为PROJECT_URL)作为相对URL。 You should only use the relative URL here ie "/hello" (and it must start with a slash), like this:
您只应在此处使用相对URL,即“ / hello”(并且必须以斜杠开头),如下所示:
public interface UserService {
@POST("/hello")
void testRequest(@Query("test") String test, Callback<String> cb);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.