【问题标题】:Api work with Postman but not in android applicationApi 与 Postman 一起工作,但不适用于 android 应用程序
【发布时间】:2019-09-08 13:56:50
【问题描述】:

一个使用 Jersey 构建的 Java 方法,它接受两个参数并存储在数据库中,它可以与邮递员一起正常工作,但是当我在 Android 应用程序中使用它时它不起作用。我尝试使用 Volley 和 Retrofit 发送请求。

服务器端代码:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/register")
public Boolean registerUser(@FormParam("userName") String userName, @FormParam("password") String password) {
    System.out.println(userName+"\t"+password);
    String insertQuery = "INSERT INTO user(user_name,password,status) VALUES(?,?,?)";
    try {
        Connection con = MyConnection.getConnection();
        PreparedStatement prst = con.prepareStatement(insertQuery);
        prst.setString(1, userName);
        prst.setString(2, password);
        prst.setInt(3, 0);
        int count = prst.executeUpdate();
        con.close();
        System.out.println(count+" Row inserted");
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

安卓代码:

public void register(final String userName, final String password) {

    User user = new User(userName, password, 1);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.1.13:8080/Demo_Application/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    JsonPlaceholderApi jsonPlaceholderApi = retrofit.create(JsonPlaceholderApi.class);

    Call<List<User>> call = jsonPlaceholderApi.register("application/x-www-form-urlencoded", user);
    call.enqueue(new Callback<List<User>>() {
        @Override
        public void onResponse(Call<List<User>> call, Response<List<User>> response) {

            if (!response.isSuccessful()){
                Log.e("Response","Something went wrong."+response.toString());
                return;
            }

            Log.d("Response",response.toString());

        }

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) {
            Log.e("Response",t.getMessage());
        }
    });
}

Postman Response

截击请求:

public void registerVolley(final String userName, final String password){

    Map<String, String> param = new HashMap<>();
    param.put("userName", userName);
    param.put("password",password);

    JsonObjectRequest arrayRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.26:8080/Demo_Application/rs/test/register", new JSONObject(param), new com.android.volley.Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.e("Response", response.toString());
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Response", error.toString());
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> param = new HashMap<>();
            param.put("userName", userName);
            param.put("password",password);

            return param;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> header =  new HashMap<>();
            header.put("Content-Type","application/json");
            return header;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(arrayRequest);

}

【问题讨论】:

  • Android 从哪里获得它的数据库连接? android调用不可能直接到registerUser,可以吗?我们将需要查看您的邮递员请求的 curl 以及 android 正在使用的 curl。我敢打赌,如果您将这两个放在一起,您将自己找到问题所在。另外,以明文形式将密码输入某些数据库?你们疯了吗?
  • 在服务器端,我无法获取用户名和密码。@FormParams 是否适用于 android 请求。
  • 请怜悯Rest API。并找到使用改造或凌空抽射的教程。您正在将 Retrofit 、 Volley 和 Httpclinet 混合在一起。
  • 当我要求使用 Volley 时,您认为它的工作原理是什么。 @SaurabhBhandari
  • 没什么好想的。用户特定的一种方式。要么使用改造或凌空抽射。不要乱七八糟

标签: java android


【解决方案1】:

您的改造代码:

JsonPlaceholderApi  jsonPlaceholderApi  = retrofit.create(JsonPlaceholderApi.class);
    Call<Boolean> call = jsonPlaceholderApi.sign("userName", "password");
    call.enqueue(new Callback<Boolean>() {
        @Override
        public void onResponse(Call<Boolean> call, Response<Boolean> response) {

            if (!response.isSuccessful()){
                Log.e("Response","Something went wrong."+response.toString());
                return;
            }

            Log.d("Response",response.toString());

        }

        @Override
        public void onFailure(Call<Boolean> call, Throwable t) {
            Log.e("Response",t.getMessage());
        }
    });

您在 jsonPlaceholderApi 中的方法:

@FormUrlEncoded
    @POST("rs/test/register")
    Call<ResponseLogin> signIn(
            @Field("userName") String userName,
            @Field("password") String password
    );

【讨论】:

  • 预期为 BEGIN_ARRAY,但在第 1 行第 6 列路径 $ 处为 BOOLEAN
  • null null com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列 'user_name' 在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)中不能为空
  • 你上面的代码是android的还是Server的?如果是android的,请分享你在postman中给出的请求,我会给你改造代码。
  • 好的。因为您的响应返回布尔值,并且您期望在 android 中列出列表。请分享您的改造代码和您在邮递员中得到的响应。
  • 看到你的服务器代码返回布尔值,你期待 > 调用。它将如何工作?
【解决方案2】:

在 proguard-rules.pro

中添加以下代码
-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keep class com.app.appname.model.** { *; }

注意:用模型文件夹更改最后一行

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 2017-10-13
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多