【发布时间】: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());
}
});
}
截击请求:
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
-
没什么好想的。用户特定的一种方式。要么使用改造或凌空抽射。不要乱七八糟