我正在尝试使用Retrofit而不是AsyncTask复制Google App Engine Servlets模块here。
我发现它很奇怪,但显然Retrofit 2.0不支持与Google App Engine的连接,如GitHub存储库中的here所述。
因此,我使用的是Retrofit 1.9和OkHttp 2.3依赖项。
我在Google Developer Console中创建了一个名为“Retrofit Test”的项目,Google为我提供了项目的URL:“http://retrofit-test-1203.appspot.com”,子域名为“http://retrofit-test-1203.appspot.com/hello。”将是我各自的Retrofit网址。以下是我的代码:
摇篮:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.troychuinard.retrofittest"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.3.0'
}
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";
private Button mTestButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mTestButton = (Button) findViewById(R.id.test_button);
//Instantiate a new UserService object, and call the "testRequst" method, created in the interface
//to interact with the server
mTestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Instantiate a new RestAdapter Object, setting the endpoint as the URL of the server
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(PROJECT_URL)
.setClient(new OkClient(new OkHttpClient()))
.build();
UserService userService = restAdapter.create(UserService.class);
userService.testRequest("Test_Name", new Callback<String>() {
@Override
public void success(String s, Response response) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
});
}
}
UserService(Retrofit)
public interface UserService {
@POST("/hello")
void testRequest(@Query("name") String name, Callback<String> cb);
}
我的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);
}
}
如您所见,我已设置项目,以便在按钮单击时发出服务器请求。与Google App Engine Servlets模块类似,我希望收到服务器的响应,然后我将其显示为Toast,其中显示“Hello +(无论输入到.testRequest()方法中的任何参数,都是”Test_Name“in这种情况。我收到以下错误,对我的方法有任何帮助/建议表示赞赏:
答案 0 :(得分:0)
这里的问题是你试图用&#34;名称&#34;发送一个POST。当您应作为@Query
发送时,参数为@Field
。
尝试这样的方法:
@FormUrlEncoded
@POST("/hello")
void testRequest(@Field("name") String name, Callback<String> cb);
答案 1 :(得分:0)
您将返回纯文本响应,并且从错误消息中我假设Retrofit需要JSON格式的响应。之一:
尝试将resp.setContentType("text/plain");
更改为resp.setContentType("application/json");
并将内容更改为JSON格式
或查看此问题以阅读回复中的字符串:Retrofit callback get response body