简体   繁体   中英

com.android.volley.parse error org.json.jsonexception value of type java.lang.string cannot be converted to jsonArray in Android Volley

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

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);

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();
                }

Content Type

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

See Returning JSON from a PHP script .

Byte Order Marker

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

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

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.

您得到的响应是一个字符串,您正在尝试将其关联到jsonArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM