Json文件不工作/不读jsonarray无法转换为jsonobject

时间:2013-11-18 02:20:35

标签: java php android json

您好我想在我的Android应用中访问我的网络记录。通过这样做,我使用JSON来做到这一点和PHP。这是我的json文件的网址:here

但问题是它只是显示php代码。我需要能够访问/读取该文件。 :(任何想法我在这里做什么?帮助非常感谢我。谢谢。

这是我到目前为止所尝试的:

    <?php 

    include('connectdb.php');
    $sql = "SELECT salesordercard_code, location_from, location_to, salesmancode FROM salesorderingcard";
    $result = mysql_query($sql);    
    if($result === FALSE) {
     die(mysql_error()); // TODO: better error handling
    }
    $set = array();
    while($row1 = mysql_fetch_assoc($result)) {
        $set[] = $row1;
    }

    header('Content-type: application/json');
    echo json_encode($set);


    ?>  

MainActivity.class

@Override
protected Void doInBackground(Void... params) {
        // Create the array 
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONFunctions.getJSONfromURL("http://www.shoppersgroup.net/vanmanagement/results.php");

        try {
            // Locate the array name
            jsonarray = jsonobject.getJSONArray("posts");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                //Log.i(MainActivity.class.getName(), jsonobject.getString("movie_name"));
                // Retrive JSON Objects
                map.put(TAG_CODE, jsonobject.getString("salesordercard_code"));
                map.put(TAG_LOCATION_FROM, jsonobject.getString("location_from"));
                map.put(TAG_LOCATION_TO, jsonobject.getString("location_to"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
}

logcat的:

            11-18 02:35:08.521: E/log_tag(1047): Error parsing data org.json.JSONException: Value [{"salesmancode":"SLMAN001","location_to":"MAIN","location_from":"IN-TRANSIT","salesordercard_code":"SLESO0001"},{"salesmancode":"SLMAN001","location_to":"MAIN","location_from":"IN-TRANSIT","salesordercard_code":"SLESO0002"}] of type org.json.JSONArray cannot be converted to JSONObject

2 个答案:

答案 0 :(得分:0)

你的results.json文件无法运行php,因为它没有被处理为PHP,它被处理为json文件,因此它将文本显示为文本。

将其重命名为results.php然后尝试,它应该打印出您需要的结果的json编码数据。

答案 1 :(得分:0)

首先作为Json String

[ // Its an Array
    "posts",// Its not an Array and its index is 0 so we will not use index 0
    {//Index1
        "salesordercard_code": "SLESO0001",
        "location_from": "IN-TRANSIT",
        "location_to": "MAIN",
        "salesmancode": "SLMAN001"
    },
    {//index2
        "salesordercard_code": "SLESO0002",
        "location_from": "IN-TRANSIT",
        "location_to": "MAIN",
        "salesmancode": "SLMAN001"
    }
]

更改你的doInBackground()方法,如下所示,

@Override
protected Void doInBackground(Void... params) {
        // Create the array 
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONFunctions.getJSONfromURL("http://www.shoppersgroup.net/vanmanagement/results.php");

        try {
            // Locate the array name
            jsonarray = new JSONArray(jsonobject.toString());

            for (int i = 1; i < jsonarray.length(); i++) {// strat from index1 
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonobj = jsonarray.getJSONObject(i);
                //Log.i(MainActivity.class.getName(), jsonobj.getString("movie_name"));
                // Retrive JSON Objects
                map.put(TAG_CODE, jsonobj.getString("salesordercard_code"));
                map.put(TAG_LOCATION_FROM, jsonobj.getString("location_from"));
                map.put(TAG_LOCATION_TO, jsonobj.getString("location_to"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
}

希望这能解决您的问题。