php - 将字符串转换为 JSON 时出错

标签 php android mysql json parsing

在 Android 上需要帮助。出现此错误 Error Parsing data org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject

下面是我运行时出现错误的页面。

public class Login extends Activity {

//URL to get User Data
private static String URL_GET = "http://10.0.2.2/android_connect/get_authentication.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_USERS = "Users";
private static final String TAG_EMAIL = "Email";
private static final String TAG_PASSWORD = "Password";

JSONParser jParser = new JSONParser();
String dbPassword = null;
// users JSONArray
JSONArray Users = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setting default screen to login.xml
    setContentView(R.layout.login);

    Button btn = (Button) findViewById(R.id.btnLogin);


    // Listening to register new account link
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            String password =  ((EditText) findViewById(R.id.password)).getText().toString();
            new getAuthentication().execute();

            if (password.equals(dbPassword)) {
                // Switching to Main screen
                Intent i = new Intent(getApplicationContext(), EchoSphere.class);
                startActivity(i);
            }
        }
    });
}
class getAuthentication extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {

        runOnUiThread(new Runnable() {
            public void run() {
                // Check for success tag
                int success;

                try {
                    String email = ((EditText) findViewById(R.id.email)).getText().toString();

                    Log.d("email:", email);

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair(TAG_EMAIL, email));

                    JSONObject json = jParser.makeHttpRequest(URL_GET, "GET", params);
                    Log.d("Password:", json.toString());

                     success = json.getInt(TAG_SUCCESS);       

                    if (success == 1) {
                        Users = json.getJSONArray(TAG_USERS);
                        JSONObject c = Users.getJSONObject(0);
                        dbPassword = c.getString(TAG_PASSWORD);
                        Log.d("DBPW:", dbPassword);
                    }

                    } catch (JSONException e) {
                    e.printStackTrace();
                    }

                    }

                });
        return null;
    }

}
}

这是 PHP 脚本,我已经验证了 PHP,它显示可以正常工作。

<?php
// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["Email"])) {
$email = $_GET['Email'];

// get a product from products table
$result = mysql_query("SELECT Email, Password FROM Users WHERE Email = '$email'");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $user[] = array();
        $user["Email"] = $result["Email"];
        $user["Password"] = $result["Password"];

        // user node
        $response["Users"] = array();

        array_push($response["Users"], $user);

        // success
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No User found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no product found
    $response["success"] = 0;
    $response["message"] = "No User found";

    // echo no users JSON
    echo json_encode($response);
}
} else {

// required field is missing

$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

结果如图

{"Users":[{"0":[],"Email":"1","Password":"123"}],"success":1}

不知道哪里出错了

最佳答案

我已经解决了错误。

谢谢大家的帮助。

我加了

header('Content-type: application/json');

到php脚本,应用程序可以运行。

关于php - 将字符串转换为 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739315/

相关文章:

php - 未找到对象!在此服务器上找不到请求的 URL

mysql - 第 462 行的 fatal error : Maximum execution time of 60 seconds exceeded in\doctrine\lib\Doctrine\Collection. php

php - 为什么这些查询这么慢?

php循环每天几次

Android 应用内购买同一类型产品的多种类型订阅

android - 如何在 BitmapFactory 中保持图像质量相同

mysql - 在数据库中存储多选调查答案的最佳方式

javascript - ajax 中的 jQuery 表单提交(带文件)

php - Yii 框架 :Form builder

java - 将用户图像从cloud firestorage检索到Imageview中