Android Volley JSON解析异常

时间:2015-07-24 14:36:12

标签: php android json android-volley

我正在尝试在Android上使用volley构建一个Login页面。 请求与GET一起工作正常,但我收到POST错误。 我在这里看了很多类似的问题,但没有解决我的问题。

我的java代码:

    final String username = user_name.getText().toString();
    final String password = pass_word.getText().toString();

    String url = "http://192.168.2.6/api_test/login.php";
    final StringBuilder resp = new StringBuilder();
    resp.append("Response: ");

    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Please wait...");
    pDialog.show();
    //RequestQueue queue = Volley.newRequestQueue(this);
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,url,null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try{
                resp_txt.setText("Response is : " + response.toString());

            }
            catch (Exception ex){
                resp_txt.setText("Error");
            }
            pDialog.hide();
        }

        }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            resp_txt.setText(error.toString());
            pDialog.hide();
    }
    }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("username", username);
            params.put("password", password);
            return params;
        }
    };

我的php代码:

    class response{
public $name = "";
    }
    $e = new response();
    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = mysqli_query($con,"SELECT name FROM users  where    username='$username' and password='$password'");
    header('Content-Type: application/json;charset=utf-8');
    if (mysqli_num_rows($result) > 0) {

    while($row = mysqli_fetch_assoc($result)) {
    $e->name = $row["name"];
    echo json_encode($e);
    }
    } 
    else{
    echo "false";
     }

我得到的错误是:

    ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted into JSON object.

与GET完美配合。

0 个答案:

没有答案