JSONException输入结束于0的字符

时间:2013-10-16 16:30:40

标签: php android json

不知怎的,我的问题似乎与我在网上查找的问题略有不同!

基本上我收到了这个错误:

10-16 12:14:03.561: E/log_tag(1271): org.json.JSONException: End of input at character 0 of 

尝试从Eclipse上的Android模拟器检索php脚本执行的以下JSON结果时:[{"id":"3"}]

从我读过的,我的问题暗示我的php脚本的JSON结果(在我的浏览器中运行正常)是空的,但是如何呢?

以下是我的MainActivity类的代码:

protected Void doInBackground(String... params) {

    //InputStream isr = null;

    // Connect to Database + Webserver
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://10.0.2.2:8080/php/AndroidTest.php"); // My php script adress                                                                    
        httppost.setHeader("Content-Type", "application/json");
        httppost.setHeader("Accept", "JSON");                                                       
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        int status = response.getStatusLine().getStatusCode();

        if(status== HttpStatus.SC_OK){
            jsonString = EntityUtils.toString(entity);
        }

        //isr = entity.getContent();
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("log_tag", "Error Connecting to Database/Webserver: " + e.toString());
    }

    //Parse JSON data   
    try {
        String s = "";
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONArray jArray = jsonObject.getJSONArray("id");

        for (int i = 0; i < jArray.length(); i++) {
            //JSONArray jArray = jArray.getJSONArray("id");
            JSONObject json = jArray.getJSONObject(i);
            s = s + "ID: " + json.getString("id");
        }
        finalResult = s;

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("log_tag", "Error Parsing JSON Data: " + e.toString());
    }

    return null;
}

protected void onPostExecute(String result) {
    //MainActivity.resultView.setText("Ergebnis: " + sResult);
    MainActivity.resultView.setText(finalResult);
}

的PHP脚本:

<?php

$con = mysql_connect("localhost","root","mcf");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("mcf", $con);

$result = mysql_query("SELECT id FROM Fragensatz1");

while($row = mysql_fetch_assoc($result))
{
    $output[]=$row;
}

print(json_encode($output));

mysql_close($con);

?>

1 个答案:

答案 0 :(得分:0)

好的,我弄清楚问题是什么: 1.我通过XAMPP运行Apache Webserver,但也运行了Tomcat服务器 2. localhost地址没有使用正确的端口,我应该使用80而不是8080!

感谢您的支持和想法!