0

我收到此错误

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

我无法确定这个错误来自哪里,我认为它会是 php,但是当我从不同的页面获取 json 时它工作正常,所以我不知道错误来自哪里

这是php页面

include("database.php");

$state = $_GET['state'];
$response["county"] = array();
switch ($state){
case "Select A State":

    break;
default:
    $squery = $con->prepare("SELECT * FROM states WHERE fullName = :state");
    $squery->bindParam(':state', $state, PDO::PARAM_STR);
    $squery->execute();
    $srow = $squery->fetch(PDO::FETCH_ASSOC);

    $tb = trim($srow['tbName']);
    $query = $con->prepare("SELECT DISTINCT(County) FROM `$tb` ORDER BY County ASC");
    $query->execute();

    while ($row = $query->fetch(PDO::FETCH_ASSOC)){
        $temp = array("display"=>$row['County']);
        array_push($response["county"], $temp);
        unset($temp);
    }
    break;
}
print json_encode($response);

这是我的java代码

package com.photosbychristian.ems;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Edit extends Activity {

private ProgressDialog stateDialog;
private ProgressDialog countyDialog;
private ProgressDialog cityDialog;

JSONParser stateParser = new JSONParser();
JSONParser countyParser = new JSONParser();
JSONParser cityParser = new JSONParser();

private static String url_state = "http://www.photosbychristian.com/ems/states.php";
private String url_county = "http://www.photosbychristian.com/ems/countys.php?state=Pennsylvania";
private static String url_city = "http://www.photosbychristian.com/ems/states.php";

private static final String TAG_STATE_DISPLAY = "display";
private static final String TAG_COUNTY_DISPLAY = "display";

JSONArray statesArray = null;
JSONArray countysArray = null;
JSONArray citysArray = null;

ArrayAdapter<String> stateAdapter;
ArrayAdapter<String> countyAdapter;
ArrayAdapter<String> cityAdapter;

Spinner state;
Spinner county;
Spinner city;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);

    TabHost th = (TabHost) findViewById(R.id.tabhost);
    th.setup();
    TabSpec specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("My Hospitals");
    th.addTab(specs);
    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("Add Hospitals");
    th.addTab(specs);
    new LoadStates().execute();

    state = (Spinner) findViewById(R.id.sState);
    county = (Spinner) findViewById(R.id.sCounty);
    city = (Spinner) findViewById(R.id.sCity);

    stateAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item);
    countyAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item);
    cityAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item);

}

class LoadStates extends AsyncTask<String, String, String> {

    // Before starting background thread Show State Progress Dialog

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        stateDialog = new ProgressDialog(Edit.this);
        stateDialog.setMessage("Loading States. Please wait...");
        stateDialog.setIndeterminate(false);
        stateDialog.setCancelable(false);
        stateDialog.show();
    }

    /** Get states form Internet **/

    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = stateParser.makeHttpRequest(url_county, "GET",
                params);

        // Check your log cat for JSON response
        Log.d("All States: ", json.toString());

        stateAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        stateAdapter.add("Select A State");
        try {
            // products found
            // Getting Array of Products
            statesArray = json.getJSONArray("state");

            // looping through All Products
            for (int i = 0; i < statesArray.length(); i++) {
                JSONObject c = statesArray.getJSONObject(i);

                // Storing each JSON item in variable
                // String id = c.getString(TAG_HID);
                String disp = c.getString(TAG_STATE_DISPLAY);

                stateAdapter.add(disp);

            }

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

        return null;
    }

    /** After completing background task Dismiss the progress dialog **/

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        stateDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // add states to spinner
                state.setAdapter(stateAdapter);

            }
        });

        state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // On selecting a spinner item
                String label = parent.getItemAtPosition(position)
                        .toString();
                if (label == "Select A State") {

                } else {
                    url_county = "http://www.photosbychristian.com/ems/countys.php?state="
                            + label;
                    Log.d("url", url_county);
                    // load county spinner

                    new LoadCounty().execute();
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
}
}

完整的堆栈跟踪

02-27 21:28:58.527: E/JSON Parser(1782): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
02-27 21:28:58.527: W/Trace(1782): Unexpected value from nativeGetEnabledTags: 0
02-27 21:28:58.527: W/Trace(1782): Unexpected value from nativeGetEnabledTags: 0
02-27 21:28:58.547: W/Trace(1782): Unexpected value from nativeGetEnabledTags: 0
02-27 21:28:58.547: W/Trace(1782): Unexpected value from nativeGetEnabledTags: 0
02-27 21:28:58.577: W/dalvikvm(1782): threadid=11: thread exiting with uncaught exception (group=0x40a70930)
02-27 21:28:58.587: E/AndroidRuntime(1782): FATAL EXCEPTION: AsyncTask #1
02-27 21:28:58.587: E/AndroidRuntime(1782): java.lang.RuntimeException: An error occured while executing doInBackground()
02-27 21:28:58.587: E/AndroidRuntime(1782):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.lang.Thread.run(Thread.java:856)
02-27 21:28:58.587: E/AndroidRuntime(1782): Caused by: java.lang.NullPointerException
02-27 21:28:58.587: E/AndroidRuntime(1782):     at com.photosbychristian.ems.Edit$LoadStates.doInBackground(Edit.java:106)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at com.photosbychristian.ems.Edit$LoadStates.doInBackground(Edit.java:1)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-27 21:28:58.587: E/AndroidRuntime(1782):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-27 21:28:58.587: E/AndroidRuntime(1782):     ... 4 more   
4

2 回答 2

0

<br听起来你有一些 HTML 处理 json 字符串。转储原始 json 字符串,您可能会发现其中嵌入了一个 PHP 错误,导致 json 语法错误。例如你正在接受

<br />Warning: blah blah blah
[your json object here]
于 2013-02-27T21:51:47.657 回答
0

您正在使用空参数执行查询,因此您的 urlhttp://www.photosbychristian.com/ems/countys.php?state=Pennsylvania实际上会变成http://www.photosbychristian.com/ems/countys.php?state=Pennsylvania?返回 -

<br />
<b>Fatal error</b>:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''' in /home/content/03/8587103/html/ems/countys.php:18
Stack trace:
#0 /home/content/03/8587103/html/ems/countys.php(18): PDOStatement-&gt;execute()
#1 {main}
  thrown in <b>/home/content/03/8587103/html/ems/countys.php</b> on line <b>18</b><br     />

要修复,请执行以下操作:

protected String doInBackground(String... args) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add( new BasicNameValuePair( "state", "Pennsylvania" ) );
    // getting JSON string from URL
    String url = "http://www.photosbychristian.com/ems/countys.php";
    JSONObject json = stateParser.makeHttpRequest(url, "GET",
            params);
于 2013-02-27T22:48:39.173 回答