doupinge9055 2016-10-18 20:41 采纳率: 100%
浏览 162

JSONException:String无法转换为JSONArray

So, I am attempting to sync the data from a table in my MySQL database to a table in a SQLite database on an Android device. Unfortunately the response String gives me a lot of formatting junk rather than the table info I need to update the table on my device. Unfortunately I cannot seem to figure out why. I get this as the response string:

I/System.out: <br />
I/System.out: <font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
I/System.out: <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\wamp\www\mysqlsqlitesync\get_tickets.php on line <i>11</i></th></tr>
I/System.out: <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
I/System.out: <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
I/System.out: <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>132632</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\mysqlsqlitesync\get_tickets.php' bgcolor='#eeeeec'>...\get_tickets.php<b>:</b>0</td></tr>
I/System.out: <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0037</td><td bgcolor='#eeeeec' align='right'>156448</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-fetch-array' target='_new'>mysql_fetch_array</a>
I/System.out: (  )</td><td title='C:\wamp\www\mysqlsqlitesync\get_tickets.php' bgcolor='#eeeeec'>...\get_tickets.php<b>:</b>11</td></tr>
I/System.out: </table></font>
I/System.out: []

And here are the exception details:

W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err:     at com.example.dw1.udpasteria.MainActivity.updateSQLite(MainActivity.java:423)
W/System.err:     at com.example.dw1.udpasteria.MainActivity$8.onSuccess(MainActivity.java:394)
W/System.err:     at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:351)
W/System.err:     at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:510)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Here is part of my MainActivity class where the exception occurs:

public void updateSQLite(String response){
    DatabaseHandler db = new DatabaseHandler(this);
    SQLiteDatabase database = db.getReadableDatabase();
    ArrayList<HashMap<String, String>> ticketSyncList = new ArrayList<>();
    // Create GSON object
    Gson gson = new GsonBuilder().create();
    try {
        // Extract JSON array from the response
        JSONArray arr = new JSONArray(response);
        System.out.println(arr.length());
        // If no of array elements is not zero
        if(arr.length() != 0){
            // Loop through each array element, get JSON object which has userid and username
            for (int i = 0; i < arr.length(); i++) {
                // Get JSON object
                JSONObject obj = (JSONObject) arr.get(i);
                System.out.println(obj.get("ticket_id"));
                System.out.println(obj.get("master_service_nr"));
                System.out.println(obj.get("ticket_nr"));
                System.out.println(obj.get("company_id"));
                // DB QueryValues Object to insert into SQLite
                queryValues = new HashMap<>();
                // Add ticket id extracted from Object
                queryValues.put("ticket_id", obj.get("ticket_id").toString());
                // Add master service number extracted from Object
                queryValues.put("master_service_nr", obj.get("master_service_nr").toString());
                // Add ticket number extracted from Object
                queryValues.put("ticket_nr", obj.get("ticket_nr").toString());
                // Add company id extracted from Object
                queryValues.put("company_id", obj.get("company_id").toString());
                // Insert User into SQLite DB
                db.insertTicket(queryValues);
                HashMap<String, String> map = new HashMap<>();
                // Add status for each User in Hashmap
                map.put("ticket_id", obj.get("ticket_id").toString());
                map.put("status", "1");
                ticketSyncList.add(map);
            }
            // Inform Remote MySQL DB about the completion of Sync activity by passing Sync status of Users
            updateMySQLSyncSts(gson.toJson(ticketSyncList));
            // Reload the Main Activity
            reloadActivity();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void syncSQLiteMySQLDB() {
    // Create AsycHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    // Http Request Params Object
    RequestParams params = new RequestParams();
    // Show ProgressBar
    prgDialog.show();
    // Make Http call to get_tickets.php
    client.post("http://...:8080/mysqlsqlitesync/get_tickets.php", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
            // Hide ProgressBar
            prgDialog.hide();
            // Update SQLite DB with response sent by get_tickets.php
            String str = new String(response, Charset.forName("UTF-8"));
            System.out.println(str);
            updateSQLite(str);
        }
        // When error occured
        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] content, Throwable error) {
            // TODO Auto-generated method stub
            // Hide ProgressBar
            prgDialog.hide();
            if (statusCode == 404) {
                Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
            } else if (statusCode == 500) {
                Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

I have spent quite a bit of time working through this, and have tried various solutions, but I think my limited knowledge of php may be hindering me. Thank you for any help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录