我在我的Android应用程序中使用Volley库。我正在尝试创建注册功能。一切似乎工作正常,我可以注册,但我无法显示PHP文件中的任何响应作为Android上的吐司。吐司甚至没有出现。也许我应该先向你展示我的代码:
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
String result = jsonResponse.getString(response);
if(result == "Registered!") {
Toast.makeText(RegActivity.this, result, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Could not register!", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegRequest regRequest = new RegRequest(username, password, email, listener);
RequestQueue queue = Volley.newRequestQueue(RegActivity.this);
queue.add(regRequest);
这是我的regRequest类:
public class RegRequest extends StringRequest {
private static String regURL = "http://chicpiq.com/android/android_register.php";
private Map<String, String> params;
public RegRequest(String username, String password, String email, Response.Listener<String> listener) {
super (Method.POST, regURL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
params.put("email", email);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
这是我的PHP:
require "../database/database.php";
$username = mysqli_real_escape_string($database, $_POST['username']);
$password = mysqli_real_escape_string($database, $_POST['password']);
$email = mysqli_real_escape_string($database, $_POST['email']);
$joindate = date("y.m.d");
$encryptpass = password_hash($password, PASSWORD_DEFAULT);
$register = mysqli_query($database, "INSERT INTO users (username, password, email, joindate) VALUES ('$username', '$encryptpass', '$email', '$joindate')");
mysqli_execute($register);
$response = "Registered!";
echo json_encode($response);
正如我所提到的,我注册没有问题,我可以这样做。我的问题是我想更改PHP代码以检查用户是否存在等等,我想显示其他消息。我似乎无法显示PHP作为Toast的响应。有没有人有任何想法? 干杯
答案 0 :(得分:0)
JSONObject js = new JSONObject();
try {
js.put("username", username);
js.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, constants.LOG_IN_URL, js,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
System.out.println("status" + response.get("status").toString().contains("success"));
if (response.get("status").toString().contains("success")) {
profileId.setText(response.getJSONArray("data").getJSONObject(0).getString("fname"));
userProfession.setText(response.getJSONArray("data").getJSONObject(0).getString("profession"));
userLocation.setText(response.getJSONArray("data").getJSONObject(0).getString("placeOfBirth"));
String url = "http://50.78.27.70:8080";
url =url.concat(response.getJSONArray("data").getJSONObject(0).getString("displayPicture"));
System.out.println("string" + response.getJSONArray("data").getJSONObject(0).getString("displayPicture"));
if(response.getJSONArray("data").getJSONObject(0).getString("displayPicture").equals("null"))
{
profilePic.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
else{
Glide.with(getBaseContext()).load(url).into(profilePic);
System.out.println("hello by default 2");
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(TimeLine.this, "Wrong Id or Password", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
});
Singleton.mInstance(TimeLine.this).addrequestqueue(request);