对本地主机服务器的简单改造 2 请求 [英] Simple retrofit2 request to a localhost server
问题描述
正在尝试制作一个可以与使用 noSQL 服务器 (app->tomcat->noSQL) 的本地主机服务器 (tomcat apache) 通信的 android 应用程序.我已经设法制作了一个 servlet 来处理get"方法上的参数并将它们正确加载到数据库中,现在我正在尝试使用retrofit2 lib从我的应用程序插入数据.按照视频和教程,我仍然无法完成这项工作.
trying to make an android app that will communicate with a localhost server (tomcat apache) which use noSQL server (app->tomcat->noSQL). I already manage to make a servlet that handle params on "get" method and load them correctly to the database, now I am trying to insert the data from my app using retrofit2 lib. following vids and tutorials I still couldnt manage to make this work.
这是我使用的界面:
public interface APIService {
@POST("login")
Call<Boolean> postUser(@Body User user);
@GET("login")
Call<Boolean> getUser(@Query("user_email") String user_email,@Query("user_pass") String user_pass);
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://localhost:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
这是我在应用程序中单击按钮时使用的代码:
and this is the code I am using when the button is clicked in the app:
APIService ApiService = APIService.retrofit.create(APIService.class);
User user = new User(name, email);
Call<Boolean> call = ApiService.getUser(email,name);
call.enqueue(new Callback<Boolean>() {
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
String ans = response.message(); //for debugging
if (ans.compareTo("yes") == 0) {
Toast.makeText(getApplicationContext(), "YES!", Toast.LENGTH_SHORT).show();
} else if (ans.compareTo("no") == 0) {
Toast.makeText(getApplicationContext(), "NO!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "ELSE?!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Boolean> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
所以 atm,当我点击按钮时什么也没有发生.(它曾经粉碎但它停止了)并且我确定正在调用按钮的功能.
so atm, nothing happen when I am clicking the button.(it used to crush but it stopped) and I am sure the the button's function is being called.
推荐答案
如果您使用的是模拟器,请将 URL 更改为 http://10.0.2.2:8080/
.
If you are using emulator, then change URL to http://10.0.2.2:8080/
.
这篇关于对本地主机服务器的简单改造 2 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!