Android JSONObject错误

时间:2017-04-03 22:33:20

标签: java php android mysql

我已注册然后我正在尝试登录,但“值&lt; br的java.lang.String类型无法转换为JSONObject”这个错误显示我不知道该怎么做,请帮助我。< / p>

这是我的Java代码:

package com.example.kamran.ebloodbank;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.Request;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class LogIn extends AppCompatActivity {

    Button btnLogin;
    EditText userName;
    EditText password;
    RequestQueue requestQueue;
    String logInUrl = "https://wwwkamranalitk.000webhostapp.com/donor_login.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

        userName = (EditText) findViewById(R.id.userName);
        password = (EditText) findViewById(R.id.password);
        btnLogin = (Button) findViewById(R.id.logInButton);
        requestQueue = Volley.newRequestQueue(getApplicationContext());

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringRequest request = new StringRequest(Request.Method.POST, logInUrl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if(success){


                                Intent intent = new Intent(LogIn.this, AfterLoginMain.class);
                                startActivity(intent);
                                finish();
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(LogIn.this);
                                builder.setMessage("Login Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> parameters = new HashMap<String, String>();
                        parameters.put("username", userName.getText().toString());
                        parameters.put("password", password.getText().toString());
                        return parameters;
                    }
                };

                RequestQueue queue = Volley.newRequestQueue(LogIn.this);
                queue.add(request);

            }
        });
    }

}

这是我的PHP代码:

<?php
    $con = mysqli_connect("localhost", "id1023450_admin", "admin", "id1023450_bloodbank");

    $username = $_POST['username'];
    $password = $_POST['password'];

    $statement = mysqli_prepare($con, "SELECT * FROM blood WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $name, $bloodgroup, $contactnumber, $emailaddress, $area, $disease, $username, $password);

    $response = array();
    $response["success"] = false;  

    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;  
       $response["name"] = $name;
        $response["bloodgroup"] = $bloodgroup;
        $response["contactnumber"] = $contactnumber;
        $response["emailaddress"] = $emailaddress;
        $responce["area"] = $area;
        $response["disease"] = $disease;
        $response["username"] = $username;
        $response["password"] = $password;
    }

    echo json_encode($response);
?>

这是Logcat:

04-04 03:28:53.112 5357-5357/com.example.kamran.ebloodbank W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.example.kamran.ebloodbank.LogIn$1$1.onResponse(LogIn.java:48)
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.example.kamran.ebloodbank.LogIn$1$1.onResponse(LogIn.java:43)
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
04-04 03:28:53.113 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at android.os.Looper.loop(Looper.java:135)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-04 03:28:53.114 5357-5357/com.example.kamran.ebloodbank W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是我的JSON回复:

{   “成功”:是的,   “name”:null,   “bloodgroup”:null,   “contactnumber”:null,   “emailaddress”:null,   “疾病”:null,   “用户名”:“kamran”,   “密码”:“abc123” }

0 个答案:

没有答案