解析数据时出错org.json.JSONException:字符0处的输入结束

时间:2014-06-01 17:55:57

标签: android json

我正在创建android应用程序,其中有一个搜索模块,用户输入数据然后发送到服务器,服务器将输出发送回用户,并以表格的形式显示。

我的问题是我收到此错误

  06-02 00:08:43.971  19902-20586/com.diamond.traders W/SingleClientConnManager﹕ Invalid use of SingleClientConnManager: connection still allocated.
    Make sure to release the connection before allocating another one.
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ org.apache.http.client.HttpResponseException: Not Found
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at com.diamond.traders.Search_result$GetDatafromDB_Searchresult.getDataFromDB(Search_result.java:676)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at com.diamond.traders.Search_result$1.run(Search_result.java:80)
06-02 00:08:43.991  19902-20586/com.diamond.traders W/System.err﹕ at java.lang.Thread.run(Thread.java:811)
06-02 00:08:43.991  19902-20586/com.diamond.traders I/System.out﹕ Exception : Not Found
06-02 00:08:43.991  19902-19902/com.diamond.traders E/log_tag﹕ Error parsing data org.json.JSONException: End of input at character 0 of

这是我的网络代码,这里是link to complete code。抱歉,因为它很长,所以无法添加。

public class GetDatafromDB_Searchresult {

        public String getDataFromDB() {


            try {

                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://192.168.0.106/test/search1.php"); // make sure the url is correct.
                //add your data
                nameValuePairs = new ArrayList<NameValuePair>(2);
                // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
                nameValuePairs.add(new BasicNameValuePair("carat1", strcolor1.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("carat2", strcolor2.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("color1", strclarity1.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("color2", strclarity2.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("cut1", strcut1.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("cut2", strcut2.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("shape1", strshape1.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("shape2", strshape2.toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("stones", strstone.toString().trim()));


                // $Edittext_value = $_POST['Edittext_value'];

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                //Execute HTTP Post Request
                response = httpclient.execute(httppost);

                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                final String response1 = httpclient.execute(httppost, responseHandler);
                System.out.println("Response : " + response1);

            } catch (Exception e) {

                System.out.println("Exception : " + e.getMessage());
            }

            return response1.trim();

        }
    }

这是我的服务器端代码

<?php
$hostname_localhost ="localhost";
$database_localhost ="testdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

 mysql_select_db($database_localhost, $localhost);

$carat1 = $_POST['carat1'];
$carat2 = $_POST['carat2'];
$color1 = $_POST['color1'];
$color2 = $_POST['color2'];
$cut1 = $_POST['cut1'];
$cut2 = $_POST['cut2'];
$shape1 = $_POST['shape1'];
$shape2 = $_POST['shape2'];
$stones = $_POST['stones'];



$query_search ="Select * from search1 where 
carats = $carat1 and carats = $carat2 and 
color = '$color1' or color =  '$color2' and 
 cut = '$cut1' or cut = '$cut2' and 
 shape = '$shape1' or shape = '$shape2' and 
 stone ='$stones'";

$query_exec = mysql_query($query_search) or die(mysql_error());


    while($row=mysql_fetch_assoc($query_exec))
            $json_output[]=$row;
      echo json_encode($json_output);

    mysql_close();

?>

编辑: 这是服务器的输出。

06-02 00:02:08.229  19428-19481/com.diamond.traders I/System.out﹕ [{"id":"1","product_code":"sdfsd","shaps":"fsdfsd","pair":"fsdf","carats":"sdgfdhn","color":"gnfgng","clarity":"jmf","service":"fgnfgnf","polish":"gnfg","symetric":"nfgnfg","tables":"nfgjkrtyr","measurments":"tjrt","flourscne":"jrtjr","description":"tjrtjgmgv","certificated":"mvbmv","ccode":"bmvb","cut":"kmty","total":"yttynyt","file":"tyfjfjgjfg"},{"id":"2","product_code":"fgn","shaps":"fgmkyh","pair":"wqqfwfw","carats":"fsfsdsdf","color":"gsdfwe","clarity":"fsdfsdf","service":"sdfs","polish":"dfsd","symetric":"fsdfs","tables":"dfsdf","measurments":"sdf","flourscne":"sdfsd","description":"fsdfsdd","certificated":"sfs","ccode":"dfsd","cut":"fsdf","total":"sdf","file":"sdf"},{"id":"3","product_code":"xfcbxcb","shaps":"xcbx","pair":"cbxcb","carats":"xcb","color":"xcbx","clarity":"cxcbxc","service":"xcb","polish":"xcbx","symetric":"cbxc","tables":"bxcb","measurments":"xcbxc","flourscne":"b","description":"xcbxcb","certificated":"xcbx","ccode":"cbx","cut":"cbxc","total":"bxc","file":"b"},{"id":"4","product_code":"xcb","shaps":"xcb","pair":"xcbxc","carats":"bxc","color":"bxc","clarity":"bxc","service":"bxc","polish":"xc","symetric":"bxcb","tables":"xcbx","measurments":"cbx","flourscne":"cbx","description":"cbxcbxcb","certificated":"xcbxc","ccode":"bxc","cut":"bxcb","total":"xcb","file":"xc"}]

1 个答案:

答案 0 :(得分:0)

您似乎没有正确收到回复。尝试这样的事情......

HttpResponse response = defaultHttpClient.execute(httppost);
InputStream data = response.getEntity().getContent();