【问题标题】:Value john of type java.lang.String cannot be converted to JSONObjectjava.lang.String 类型的值 john 无法转换为 JSONObject
【发布时间】:2015-12-16 11:45:46
【问题描述】:

我尝试构建一个将参数发送到 php 代码作为 where 的 android 应用程序

该php代码中的查询条件,我应该在log.i中获取查询结果,但是

我收到以下错误

org.json.JSONException:java.lang.String 类型的值 john 无法转换为 JSONObject

主活动

 public class MainActivity extends Activity {

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

        try {
            JSONObject toSend = new JSONObject();
            toSend.put("msg", "3");

            JSONTransmitter transmitter = new JSONTransmitter();
            transmitter.execute(new JSONObject[] {toSend});

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

    }

} 

JSONTransmitter 类。

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {

    String url = "http://192.168.1.10:89/b.php";


    protected JSONObject doInBackground(JSONObject... data) {
        JSONObject json = data[0];
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        JSONObject jsonResponse = null;

        HttpPost post = new HttpPost(url);
        try {
            StringEntity se = new StringEntity("json="+json.toString());
            post.addHeader("content-type", "application/x-www-form-urlencoded");
            post.setEntity(se);

            HttpResponse response;
            response = client.execute(post);
            String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());

            jsonResponse=new JSONObject(resFromServer);
            Log.i("Response from server", jsonResponse.getString("msg"));
            Toast.makeText(null, resFromServer, Toast.LENGTH_LONG);
        } catch (Exception e) { e.printStackTrace();}

        return jsonResponse;
    }

}

php代码

<?php
 mysql_connect("localhost", "root", "password") 
or die(mysql_error());
mysql_select_db("test") or die(mysql_error());


$result = mysql_query(" select  part_name from  Services_parts where  part_id=  1 ") 
or die(mysql_error());  

$row = mysql_fetch_array( $result );


$j_out = new stdClass();
$j_out->part_name= $row['part_name'];

echo json_encode($j_out);
?>

【问题讨论】:

标签: php android json android-json


【解决方案1】:

你执行的代码相当于这个:

JSONObject obj = new JSONObject("john");

明显导致异常的原因

这意味着您来自服务器的 json 数据格式错误。问题可能出在您的 php 代码中,用于在 json 中返回的项目数组适应 php+java 代码:

$res_items = array();
for ( ... )  {
    array_push($res_items, $some_item);
}
$js_result = get_json_encode(array(
                "results" => $res_items
            ));
echo $js_result;

然后在java代码中:

JSONObject jsResp = new JSONObject(serverJsonResult);
JSONArray results = jsResp.getJSONArray("results");
for (int n = 0; n < results.length(); ++n) {
   JSONObject js_row = results.getJSONObject(n);
   // ...
}

【讨论】:

  • 为什么在 java 代码中将 JSONObject 转换为 JSONArray 然后获取该数组的元素该数组的元素是什么?它只是一个元素约翰,请完成您的代码以尝试它们
  • @JEREEF 我不明白你,你的服务器必须返回有效的 json 数据,这个异常被抛出是因为它没有返回它。最简单的带有 'john' 的 json 格式看起来像 {name:"john"}
猜你喜欢
  • 1970-01-01
  • 2017-02-03
  • 2017-10-02
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多