java - 需要通过 Retrofit 连接到 Google App Engine 的帮助

标签 java google-app-engine android-studio retrofit

我已成功遵循 Google 教程 here ,使用 Android Studio Servlets 模块连接到 Google App Engine。我能够在我的设备上看到 Toast 消息,这意味着我成功连接到服务器并收到响应。

我注意到这个模块使用 AsyncTask 来处理后台任务。据我了解,Retrofit 是一种在后台线程中处理任务的更简单且有效的方法。我基本上是尝试使用 Retrofit 1.9.0 而不是他们提供的 ServletPostAsyncTask Java 类来复制上面提到的 Google 教程。

下面是我的代码:

主要 Activity :

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();
        }
    });

  }
}

Retrofit 所需的 UserService 接口(interface):

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);


}

我的 Servlet,根据 Google 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);
  }
}

在我的 userService.testRequest() 方法中,我传入“Test_Name”作为字符串参数。这段文本是我希望传递给服务器的内容,然后看到一个显示“Hello Test_Name”的 toast(收到服务器响应后),就像 Google App Engine Servlets 模块所解释的那样。

现在,我收到以下错误:

enter image description here

由于文档有限,因此我们欢迎任何关于将 Retrofit 与 Google App Engine 结合使用的建议。

最佳答案

首先,您的基本网址应该只是网站的域名,即 http://retrofit-test-1203.appspot.com (它不应该包含您尝试访问的资源的路径)因此可以这样声明:

private static String PROJECT_URL = "http://retrofit-test-1203.appspot.com"

其次,不要使用基本 url(本例中为 PROJECT_URL)作为 UserService 接口(interface)声明中的相对 URL。您只能在此处使用相对 URL,即“/hello”(并且必须以斜杠开头),如下所示:

public interface UserService {

   @POST("/hello")
   void testRequest(@Query("test") String test, Callback<String> cb);


   }

关于java - 需要通过 Retrofit 连接到 Google App Engine 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045684/

相关文章:

java - hudson 与特拉克

java - 服务以错误的时间间隔执行短信

java - 显示具有两个面板的 GUI,每个面板包含三个按钮

python - 在 Google Cloud Storage 中存储多个同名文件?

java - Android handle 软回车键

python - 访问 python 类实例变量时出错

java - Jetty不兼容类更改错误: Implementing class

java - NetworkSecurityConfig : No Network Security Config specified -- Android 7. 0 错误?

java - AndroidManifest 不匹配

android - 是否可以将现有的 Android APK 转换为 bundle