Php 分析数据org.json.JSONException时出错:值<;无法将java.lang.String类型的br转换为JSONArray

Php 分析数据org.json.JSONException时出错:值<;无法将java.lang.String类型的br转换为JSONArray,php,android,json,Php,Android,Json,我正在创建一个应用程序,其中用户输入数据进行搜索,然后应用程序将其发送到服务器,然后服务器将搜索结果发送回客户端 我犯了错误,我无法理解它的意思。我正在从服务器获得准确的结果 这是我在那个应用程序中的网络代码,这是 公共类GetDatafromDB\u Searchresult{ 字符串响应1; 公共字符串getDataFromDB(){ httpclient=新的DefaultHttpClient(); httppost=新的httppost(“http://192.168.0.106/tes

我正在创建一个应用程序,其中用户输入数据进行搜索,然后应用程序将其发送到服务器,然后服务器将搜索结果发送回客户端

我犯了错误,我无法理解它的意思。我正在从服务器获得准确的结果

这是我在那个应用程序中的网络代码,这是

公共类GetDatafromDB\u Searchresult{
字符串响应1;
公共字符串getDataFromDB(){
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://192.168.0.106/test/search.php“”;//请确保url正确无误。
//添加您的数据
nameValuePairs=新的ArrayList();
//发布时始终使用相同的变量名,即android端变量名和php端变量名应相似,
添加(新的BasicNameValuePair(“carat1”,strcart1.toString().trim());
添加(新的BasicNameValuePair(“carat2”,strcarat2.toString().trim());
添加(新的BasicNameValuePair(“carat1”,strcolor1.toString().trim());
添加(新的BasicNameValuePair(“carat2”,strcolor2.toString().trim());
添加(新的BasicNameValuePair(“clarity1”,strCarity1.toString().trim());
添加(新的BasicNameValuePair(“clarity2”,strCarity2.toString().trim());
添加(新的BasicNameValuePair(“cut1”,strut1.toString().trim());
添加(新的BasicNameValuePair(“cut2”,strut2.toString().trim());
添加(新的BasicNameValuePair(“shape1”,strshape1.toString().trim());
添加(新的BasicNameValuePair(“shape2”,strshape2.toString().trim());
添加(新的BasicNameValuePair(“stones”,strstone.toString().trim());
//$Edittext_value=$_POST['Edittext_value'];
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
试一试{
response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
{
如果(实体!=null)
{
entity.consumercontent();
}
}
ResponseHandler ResponseHandler=新BasicResponseHandler();
response1=httpclient.execute(httppost,responseHandler);
System.out.println(“响应:+response1”);
}捕获(例外e){
e、 printStackTrace();
System.out.println(“异常:+e.getMessage());
}
返回响应1.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'];
$clarity1 = $_POST['clarity1'];
$clarity2 = $_POST['clarity2'];
$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 Between '$carat1' and '$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();

?>

如果要从数据库返回HTML代码,则应更改数据库中语句的结果

问题: 您忘了将
color
参数放在这个php参数的
NameValuePairs

    $color1 = $_POST['color1'];
$color2 = $_POST['color2'];

您的服务器没有返回JSON。您正在尝试将HTML解析为JSON。这肯定会有例外。你能告诉我我需要做什么改变吗??我正在为我的其他类使用类似的代码,它工作得非常好。对不起,我只知道
Android
。我可以给你指路,但不能告诉你怎么做。您必须搜索,如何从服务器返回
JSON
数据。这很好。只要告诉我这些步骤,我就会做改变。你能告诉我怎么做吗?我正在为其他类使用类似的服务器端,它工作得很好。@SamvidKulkarni所以当你在PC的网站上尝试它时,它会以json格式返回?我没有网站。我的客户端是我的android应用程序,输出仅显示在android应用程序上。此外,格式必须为JSON。@SamvidKulkarni在数据库中尝试您的语句并检查其中的结果。我确实通过在phpmyadmin中运行查询来检查我的语句,我得到了完美的结果。
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: color1 in C:\wamp\www\test\search.php on line <i>16</i></th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders 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>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>147496</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\test\search.php' bgcolor='#eeeeec'>..\search.php<b>:</b>0</td></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ </table></font>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <br />
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: color2 in C:\wamp\www\test\search.php on line <i>17</i></th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders 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>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>147496</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\test\search.php' bgcolor='#eeeeec'>..\search.php<b>:</b>0</td></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ </table></font>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ [{"id":"4","product_code":"4","shape":"Asscher","pair":"jgj","carats":"1.8","color":"G","clarity":"VS2","service":"iuhi","polish":"hiihi","symetric":"hih","tables":"ihi","measurments":"hi","flourscne":"hi","description":"ih","certificated":"hjgj","ccode":"hjgj","cut":"Signature Ideal","total":"iuhiu","file":"hiuhih","stone":"Single"}]
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <br />
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: color1 in C:\wamp\www\test\search.php on line <i>16</i></th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders 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>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>147496</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\test\search.php' bgcolor='#eeeeec'>..\search.php<b>:</b>0</td></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ </table></font>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <br />
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: color2 in C:\wamp\www\test\search.php on line <i>17</i></th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders 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>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>147496</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\test\search.php' bgcolor='#eeeeec'>..\search.php<b>:</b>0</td></tr>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ </table></font>
06-06 12:10:19.825    1237-1566/com.diamond.traders I/System.out﹕ [{"id":"4","product_code":"4","shape":"Asscher","pair":"jgj","carats":"1.8","color":"G","clarity":"VS2","service":"iuhi","polish":"hiihi","symetric":"hih","tables":"ihi","measurments":"hi","flourscne":"hi","description":"ih","certificated":"hjgj","ccode":"hjgj","cut":"Signature Ideal","total":"iuhiu","file":"hiuhih","stone":"Single"}]
06-06 12:10:19.825    1237-1237/com.diamond.traders E/log_tag﹕ Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
    $color1 = $_POST['color1'];
$color2 = $_POST['color2'];