JSONException:无法将 java.lang.String 类型的值转换为 JSONArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19244516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:24:04  来源:igfitidea点击:

JSONException: Value <br of type java.lang.String cannot be converted to JSONArray

javaandroid

提问by elfoz

I know that this kind of problem has some solutions and applied some solutions for this problem but I can't solve and i'm confused. Please help me. Here is code:

我知道这种问题有一些解决方案,并为这个问题应用了一些解决方案,但我无法解决,我很困惑。请帮我。这是代码:

protected String doInBackground(Boolean... params) {

        String result = null;

        StringBuilder sb = new StringBuilder();


        try {

            // http post
            HttpClient httpclient = new DefaultHttpClient();



            HttpGet httppost = new HttpGet(
                    "http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password");  

            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() != 200) {
                Log.d("MyApp", "Server encountered an error");
            }



            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "utf-8"), 8); //old charset iso-8859-1

            sb = new StringBuilder();

            sb.append(reader.readLine() + "\n");

            String line = null;

            while ((line = reader.readLine()) != null) {

                sb.append(line + "\n");


            }
            result = sb.toString();

            Log.d("test", result);

        } 
        catch (Exception e) {

        Log.e("log_tag", "Error converting result " + e.toString());

        }

        return result;
    }

PHP CODE:

代码:

$login=$_GET["login"]; 
$user_name=$_GET["user_name"]; 
$password=$_GET["password"]; 
$output=array(); 
if ($login) { 
$sql=mysql_query("SELECT user_id FROM users WHERE user_name='".$user_name."' AND user_pass='".$password."' "); 

while($row=mysql_fetch_array($sql)) { 
 $user_id=$row["user_id"]; 
} 

$sql=mysql_query("SELECT name,device_id,lat,lon FROM devices WHERE user_id='".$user_id."' LIMIT 100"); 

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

print(json_encode($output)); 
mysql_close();

The logcat:org.json.JSONException: Value

logcat:org.json.JSONException: Value

What should i do? The Php code runs on web page without any errors but why got an error in this part? In logcat also has: ( ! ) Notice: Undefined variable: user_id in C:\wamp\www\getProducts.php on line 27

我该怎么办?Php 代码在网页上运行没有任何错误,但为什么在这部分出现错误?在 logcat 中也有:(!)注意:未定义变量:user_id in C:\wamp\www\getProducts.php on line 27

Logcat!

逻辑猫!

<br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0'    cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: user_id in C:\wamp\www\getProducts.php on line <i>27</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>... this kind of code appears in logcat then org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray at org.json.JSON.typeMismatch(JSON.java:107)

采纳答案by A.S.

You do not return a valid JSON, seems that you are returning a HTML snippet with <br>have a look at your returning source of your website.

您没有返回有效的 JSON,似乎您正在返回一个 HTML 片段,<br>并查看您网站的返回源。

What should i do? The Php code runs on web page without any errors but why got an error in this part? In logcat also has: ( ! ) Notice: Undefined variable: user_id in C:\wamp\www\getProducts.php on line 27

我该怎么办?Php 代码在网页上运行没有任何错误,但为什么在这部分出现错误?在 logcat 中也有:( ! ) 注意:未定义变量:C:\wamp\www\getProducts.php 中的 user_id 第 27 行

So the user_idis not defined! Just look at your GET/POST Parameters or show us your relevant php code.

所以user_id没有定义!只需查看您的 GET/POST 参数或向我们展示您的相关 php 代码。

Please use echo json_encode($output);not print.

echo json_encode($output);不要使用print

And what are you doing if there is no returning row from your first Query. Try this.

如果您的第一个 Query 没有返回行,您在做什么?尝试这个。

$sql=mysql_query("SELECT user_id FROM users WHERE user_name='".$user_name."' AND user_pass='".$password."' "); 
if(mysql_num_rows($sql) == 0){
   echo "USERID CANNOT BE FOUND";
}
while($row=mysql_fetch_array($sql)) { 
  echo "USERID FOUND" .$row["user_id"] ;
  $user_id=$row["user_id"]; 
} 

It seems that user_namecould not be found. So just debug it there by trying to echo your user_id after the while. If you do not get the UserID FOUND echo the user_name and password do not exist.

好像user_name找不到。因此,只需在一段时间后尝试回显您的 user_id 来调试它。如果您没有找到 UserID FOUND 回显,则 user_name 和密码不存在。

Edit: As your logact says, there is no Entry for the username you are passing so the $user_id will never be filled.

编辑:正如您的 logact 所说,您传递的用户名没有条目,因此永远不会填充 $user_id。

http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password

http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password

You are passing user_name = UserName and password = Password. Does this entry really exist in your Database?

您正在传递 user_name = UserName 和 password = Password。这个条目真的存在于您的数据库中吗?

Also for better performance have a look at MySQL LETF JOIN and MySQL subqueries.

另外,为了获得更好的性能,请查看 MySQL LETF JOIN 和 MySQL 子查询。