【问题标题】:Simple retrofit2 request to a localhost server对本地主机服务器的简单改造 2 请求
【发布时间】:2017-02-25 22:30:36
【问题描述】:

尝试制作一个将与使用 noSQL 服务器 (app->tomcat->noSQL) 的 localhost 服务器 (tomcat apache) 通信的 android 应用程序。我已经设法制作了一个处理“get”方法上的参数并将它们正确加载到数据库的servlet,现在我正在尝试使用retrofit2 lib从我的应用程序中插入数据。 按照视频和教程,我仍然无法做到这一点。

这是我正在使用的界面:

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

这是我在应用程序中单击按钮时使用的代码:

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,当我单击按钮时没有任何反应。(它曾经粉碎但它停止了)并且我确定正在调用按钮的函数。

【问题讨论】:

    标签: java android tomcat retrofit2 nosql


    【解决方案1】:

    如果您使用的是模拟器,请将 URL 更改为 http://10.0.2.2:8080/

    【讨论】:

    • 该应用已安装在我的手机上
    • 然后使用你电脑的IP地址
    【解决方案2】:

    解决问题的两种方法

    1. 如果您使用模拟器运行,请使用 URL 作为http://10.0.2.2:8080/ 而不是本地主机

    2. 从移动应用运行使用 PC IP 地址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2014-11-03
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多