簡體   English   中英

需要幫助進行翻新連接到Google App Engine

[英]Need Help Connecting to Google App Engine With Retrofit

我已成功使用Android Studio Servlets模塊連接到Google App Engine,並在這里成功遵循了Google教程。 我能夠在設備上看到Toast消息,這意味着我已成功連接到服務器並收到了響應。

我注意到該模塊使用AsyncTask來處理后台任務。 據我了解,Retrofit是一種在后台線程中處理任務的更為簡單有效的方法。 我基本上是在嘗試使用Retrofit 1.9.0(而不是他們提供的ServletPostAsyncTask Java類)復制上面提到的Google教程。

下面是我的代碼:

主要活動:

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接口:

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


}

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

在我的userService.testRequest()方法中,我傳入“ Test_Name”作為字符串參數。 這段文字是我希望傳遞給服務器的內容,然后看到顯示“ Hello Test_Name”的祝酒詞(收到服務器響應后),就像Google App Engine Servlets模塊說明的那樣。

現在,我收到以下錯誤:

在此處輸入圖片說明

由於文檔數量有限,因此不建議將Retrofit與Google App Engine一起使用。

首先,您的基本url應該只是站點的域,即http://retrofit-test-1203.appspot.com (它不應包含您要訪問的資源的路徑),因此請聲明為這個:

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

其次,不要在UserService接口的聲明中使用基本url(在這種情況下為PROJECT_URL)作為相對URL。 您只應在此處使用相對URL,即“ / hello”(並且必須以斜杠開頭),如下所示:

public interface UserService {

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


   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM