1

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();
                }
1
  • can you show your json structure? Commented Dec 10, 2014 at 11:58

2 Answers 2

1

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 includes 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.

Sign up to request clarification or add additional context in comments.

9 Comments

Yes, you're getting what appears to be JSON data, but it doesn't conform to RFC 4627 (JSON specification). Maybe you can use a JSON validator to find the offending content. If you're still getting the same error message, try using as String response listener Response.Listener<String> instead of Response.Listener<JSONArray>.
but i will receive json array , if i receive string how can i do parsing. example: JsonObject str=news JsonObject(response); JsonArray array=str.getJsonArray(); this is mistake.
I agree, it would be a mistake. The invalid characters are the first three characters in the response body, before the JSON array starts. They are BOM (byte order marker) from PHP. For more information, find UTF-8 BOM in this answer stackoverflow.com/questions/8028957/…
how can i edit character problem in php? i understood, normally i received json data but its telling me string because of character problem.
Save your .php source file (and any includes) with ASCII encoding instead of unicode.
|
0

The response you are getting is a string and you are trying to assing it to jsonArray

1 Comment

i know this print is string, but how can i create json data in php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.