当前位置: 首页 > 知识库问答 >
问题:

分析数据组织时出错。json。JSONException:java类型的值

白禄
2023-03-14

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

我得到了错误,我不能理解它的意思。我从服务器得到确切的结果。

这是我的网络代码从那里的应用程序和这里是链接到完整的代码

     public class GetDatafromDB_Searchresult {
            String response1 ;


            public String getDataFromDB() {
 httpclient = new DefaultHttpClient();
                    httppost = new HttpPost("http://192.168.0.106/test/search.php"); // make sure the url is correct.
                    //add your data
                    nameValuePairs = new ArrayList<NameValuePair>();
                    // 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", strcarat1.toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair("carat2", strcarat2.toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair("carat1", strcolor1.toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair("carat2", strcolor2.toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair("clarity1", strclarity1.toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair("clarity2", 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


                try {
                        response = httpclient.execute(httppost);
                    HttpEntity entity= response.getEntity();
                    {
                        if(entity!=null)
                        {
                            entity.consumeContent();
                        }
                    }

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

                } catch (Exception e) {
                    e.printStackTrace();
                    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'];
$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();

?>

我还更改了echo json_编码($json_输出);打印(json_编码($json_输出));但产出仍然没有变化

这是我从logcat得到的输出。

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

共有1个答案

邢飞鸿
2023-03-14

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

问题:你忘了你把颜色参数放在你的NameValuePair从这个php参数

    $color1 = $_POST['color1'];
$color2 = $_POST['color2'];
 类似资料:
  • 我试图实现一个简单的android应用程序,在这个应用程序中,我试图通过php站点检索数据库值,但是下面的代码在解析json数据时产生错误。 我的php文件编码json如下 Android类如下 最后是logcat输出 如何获得表的所需值 请帮忙。

  • 我正在尝试登录应用程序,但我一直遇到“解析数据org.json.JSONException错误:在字符0的输入结束” 我的登录名。java看起来像这样 公共类登录扩展活动实现OnClickListener{私有EditText用户,pass; 私有静态最终字符串LOGIN_URL="my url"; //从真实服务器进行测试: } } 我的JSONParser。java如下所示:` 公共类JSON

  • 我有一个实体User,它有两个Data类型的字段。 我有一个带有save()方法的Controller,它将用户的数据保存到postgesql数据库中。 我有一个JSON,我将它发送到controller { "name":"Max "," createdOn":"2019-07-26 11:13:39 "," modified on ":" 2019-07-26 11:13:39 " } 最后在

  • 我试图解析来自服务器的响应json。数据是一个对象数组,但有时服务器在数组的项之间发送一个布尔值。像这样: 正如你所看到的,他们之间有一个错误。 tnx提前。

  • 问题内容: 我目前有一个表,其列为。该列可以容纳数字或文本。在某些查询期间,我将其视为一列(我将其与另一个表中的列进行连接) 只要在该字段中只有数字没有问题,但是即使是一分钟的分钟也有文本,而在此字段中没有数字,我就会收到“将数据类型转换为的错误”。即使在WHERE部分中,我也确保所有文本字段都没有出现。 为了解决这个问题,我创建了一个视图,如下所示: 但是,即使视图仅显示具有数字值的行并将Myc

  • 我试图解析JSON数据,为此我编写了自定义模式。通过添加模式或不添加模式来解析数据时,我得到以下错误: 这是我的示例数据: 这是我的模式: 以下是我尝试获取数据的方式: 提前致谢。