[英]I want to simply send get request through android but it doesn't work my code is as follow,
[英]Why doesn't android studio follow through the response listener?
我在 Android Studio 中有一個簡單的注冊腳本。 我將 php 腳本設置為在代碼正確運行時發送響應。 Android studio 應該收到響應,但沒有。
這是有問題的 PHP 塊;
$response = array();
$response["success"] = true;
echo $response;
這是有問題的 CreateUser 塊;
createuser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = username1.getText().toString();
final String password = password1.getText().toString();
final String isadmin = isAdmin.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response Value: ", response);
if (response.equals("success")){
Intent intent = new Intent(CreateUser.this, MainActivity.class);
CreateUser.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry",null)
.create()
.show();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener);
RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
queue.add(registerRequest);
}
});
}
這是注冊請求;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://192.168.*.*:80/phptesting/Register.php";
private Map<String, String> params;
public RegisterRequest(String username, String password,String isAdmin, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL,listener,null);
params = new HashMap<>();
params.put("username",username);
params.put("password",password);
params.put("isAdmin",isAdmin+"");
}
您不能在 PHP 中回顯數組。 當你這樣做時,輸出將是
PHP 注意:第 1 行 php shell 代碼中數組到字符串的轉換
大批
您需要直接發送字符串“成功”,例如
echo "success";
或者將數組序列化為 JSON,但隨后您將不得不更改您的ResponseListener
以便它可以理解這一點,可能使用 Jackson 或 Android 中可用的任何內容。
更好的替代方法是不讀取實際的響應正文,而只是設置適當的 HTTP 狀態代碼來指示成功或失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.