0

不知何故,我的问题似乎与我可以在网上查找的问题略有不同!

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

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);

?>
4

1 回答 1

0

好的,我发现了问题所在: 1. 我正在通过 XAMPP 运行 Apache Webserver,但是也有一个 Tomcat 服务器在运行 2. localhost 地址没有使用正确的端口,我应该使用 80 而不是8080!

感谢您的支持和想法!

于 2013-10-17T14:47:24.770 回答