代码之家  ›  专栏  ›  技术社区  ›  XpressGeek

Android截取字符串响应未转换为JsonObject

  •  0
  • XpressGeek  · 技术社区  · 6 年前

    字符串响应 从服务器(在PHP上运行并发送JsonObject响应的服务器后端),然后将其转换为 JSO对象 安卓截击 显示以下错误:

    org.json.JSONException异常:类型的值dbjava.lang.String文件无法转换为JSONObject

         public void loginFunction(String a, String b) {
    
            progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
            progressDialog.show();
    
            final String user_email_insert = a;
            final String user_password_insert = b;
            String HttpUrl = getString(R.string.server_name)+getString(R.string.log_in_api);
    
            StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String ServerResponse) {
    
                            progressDialog.dismiss();
                            try {
    
                                JSONObject person = new JSONObject(ServerResponse);
                                String name = person.getString("user_full_name");
                                String email = person.getString("user_email");
                                Log.d("1111",person.toString());
    
                                Toast.makeText(getApplicationContext(), name+"\n"+email, Toast.LENGTH_LONG).show();
    
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                            }
    
    
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
    
    
                            progressDialog.dismiss();
    
                            Toast.makeText(LogIn.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
    
    
                    Map<String, String> params = new HashMap<String, String>();
    
                    params.put("user_email", user_email_insert);
                    params.put("user_password", user_password_insert);
    
    
                    return params;
                }
    
            };
    
    
            RequestQueue requestQueue = Volley.newRequestQueue(LogIn.this);
            requestQueue.add(stringRequest);
    
        } 
    

    PHP代码是:

            <?php 
    
    
    
        include "db_connection.php";
    
       $user_email = $_POST["user_email"];
    
       $user_password = $_POST["user_password"];
    
    
       $sql = "SELECT user_password,user_full_name,user_email FROM user_info WHERE user_email ='" .$user_email. "'";
       mysqli_query($con,'SET CHARACTER SET utf8');
       mysqli_query($con,"SET SESSION collation_connection ='utf8_general_ci'");
    
       $res = mysqli_query($con,$sql);
    
       $row = mysqli_fetch_row($res);
    
       if($user_password == $row[0]){
    
       // echo "Login successfull".$row[1];
    
    $myObj->user_full_name = $row[1];
    $myObj->user_email = $row[2];
    
    
    $myJSON = json_encode($myObj,JSON_UNESCAPED_UNICODE);
    
    echo $myJSON;
    
    
    
         //echo "Login successfull";
    
    
    
       }else{
    
         echo "Wrong password";
         //header( 'Location: login_failed.php' ) ;
    
       }
    
    
    
       mysqli_close($con);
    
    
    
    
      ?> 
    
    0 回复  |  直到 6 年前