com.android.volley.parse错误java.lang.string类型的org.json.jsonexception值无法在Android Volley中转换为jsonArray [英] com.android.volley.parse error org.json.jsonexception value of type java.lang.string cannot be converted to jsonArray in Android Volley

查看:39
本文介绍了com.android.volley.parse错误java.lang.string类型的org.json.jsonexception值无法在Android Volley中转换为jsonArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行该程序时,出现此错误.我不知道该怎么解决.请帮我找到它.

When I run this program I get this error . I don't know how to solve . Help me finding it Please.

这是我的json_encode php代码api.

This is my json_encode php code api.

  $i=0;
  while ($row = $result->fetch_assoc())
  {
   $array[$i]=array(
        "news_id" => $row["news_id"], 
        "news_title" => $row["news_title"], 
        "news_abstract" => $row["news_abstract"], 
        "news_content" => $row["news_content"], 
        "news_date" => $row["news_date"], 
        "news_link" => $row["news_link"], 
        "news_image_link" => $row["news_image_link"], 
        "sources_name" => $row["sources_name"], 
        "category_name" => $row["category_name"],
        "news_visible" => $row["news_visible"]
    );
    $i++;
  }
    $json=json_encode($array, JSON_HEX_TAG|JSON_HEX_APOS);

和我的Android Volley JsonArrayRequest的JsonArrayRequest.

and my JsonArrayRequest of Android Volley JsonArrayRequest.

JsonArrayRequest newsRequest = new JsonArrayRequest( Url.getUrlJson(), new  Response.Listener<JSONArray>(){
        @Override
        public void onResponse( JSONArray response ){
            Log.d( TAG, response.toString() );
            hidePDialog();

            // Parsing json
            for( int i = 0; i < response.length(); i++ ){
                try{
                    JSONObject obj = response.getJSONObject( i );
                    News news = new News();
                     if( "1".equals( obj.getString( "news_visible" )) ){

                        news.setNews_id( obj.getString( "news_id" ) );
                        news.setNews_title( obj.getString( "news_title" ) );
                        news.setNews_abstract( obj.getString( "news_abstract" ) );
                        news.setNews_content( obj.getString( "news_content" ) );
                        news.setNews_date( obj.getString( "news_date" ) );
                        news.setNews_link( obj.getString( "news_link" ) );
                        news.setNews_image_link( obj.getString( "news_image_link" ));
                        news.setSources_name( obj.getString( "sources_name" ) );
                        news.setCategory_name( obj.getString( "category_name" ) );
                    }
                    newsList.add( news );
                }
                catch( JSONException e ){
                    e.printStackTrace();
                }

推荐答案

内容类型

<?PHP
header('Content-Type: application/json');

请参见从PHP脚本中返回JSON .

字节顺序标记

PHP倾向于在响应开始时返回 BOM .

PHP is prone to return a BOM at the beginning of the response, when the .php file or any of its includes have UTF-8 character encoding.

有关更多信息,请参见此W3C答案和本帖子中的 UTF-8 BOM部分.

For more information, see this W3C answer and section UTF-8 BOM in this post.

一种建议的防止PHP在BOM之前添加的方法是将您的php文件的字符集从UTF-8更改为ASCII或ISO-8859-15.

One suggested way to prevent PHP from prepending a BOM is to change the character set of your php file(s) from UTF-8 to ASCII or ISO-8859-15.

这篇关于com.android.volley.parse错误java.lang.string类型的org.json.jsonexception值无法在Android Volley中转换为jsonArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆