【问题标题】:org.json.JSONException: Value Array of type java.lang.String cannot be converted to JSONObjectorg.json.JSONException:java.lang.String 类型的值数组无法转换为 JSONObject
【发布时间】:2013-12-18 18:49:50
【问题描述】:

我通过 JSON 使用 php 包装器从服务器端返回值。但是当我将值返回给客户端时会出现以下错误。

这是我的客户端代码

        @Override
    protected Boolean doInBackground(String... arg0) {

        try {

            // Setup the parameters
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("FirstNameToSearch",
                    strNameToSearch));
            // Create the HTTP request
            HttpParams httpParameters = new BasicHttpParams();

            // Setup timeouts
            HttpConnectionParams
                    .setConnectionTimeout(httpParameters, 45000);
            HttpConnectionParams.setSoTimeout(httpParameters, 45000);

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost(
                    "http://172.16.12.142/etsmobile/menuload.php");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity);       

            // Create a JSON object from the request response
            JSONObject jsonObject = new JSONObject(result);

            // Retrieve the data from the JSON object
            pasName = jsonObject.getString("Name");
            pasPost = jsonObject.getString("Post");
            pasStation = jsonObject.getString("Station");


        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return true;
    }

这是我的服务器端代码

<?php

$firstname = $_POST["FirstNameToSearch"];

$con = mysql_connect("localhost", "root", "") or die("Unable to connect to MySQL");


if (mysqli_connect_errno()) {
    echo 'Database connection error: ' . mysqli_connect_error();
    exit();
}

$selected = mysql_select_db("ets", $con) or die("Could not select ets");

$userdetails = mysql_query("SELECT users.* FROM login, users WHERE username = '$firstname' and login.emp_no=users.emp_no");
$getUser_result = mysql_fetch_assoc($userdetails);


$name = $getUser_result['name'];
$post = $getUser_result['post'];
$station = $getUser_result['station'];

mysql_close($con);

$result_data = array('Name' => $name, 'Post' => $post, 'Station' => $station);
//print_r($result_data);
echo json_encode($result_data);
?> 

这是我的 JSON 输出

{"Name":"Sameera Yatawara","Post":"Station Master","Station":"Dematagoda"} 

【问题讨论】:

  • 响应是字符串而不是 jsonobject
  • @SotiriosDelimanolis 我发布了我的 JSON。
  • @Raghunandan 我在回显结果集时已将字符串转换为 JSON。
  • 你的db连接参数OK吗?
  • @Leonardo 是的。它们没问题,SQL 返回值。

标签: java php android json


【解决方案1】:

尝试使用以下方式检索数据:

...
String result = EntityUtils.toString(entity);

JSONArray array = (JSONArray) new JSONTokener(result).nextValue(); 
JSONObject json = array.getJSONObject(0);
json.getString('name');
...

...
String result = EntityUtils.toString(entity);

JSONObject json = (JSONObject ) new JSONTokener(result).nextValue();
json.getString('name');
...

还要检查 PHP 脚本编码。我有类似的问题。我在文本编辑器(Notepad++,..)中使用 DOM 将编码设置为 UTF-8,它开始工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多