Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

php - How to get json Array not html from URL in android

I am trying to get json Array from url in android using volley but it is showing following error

com.android.volley.ParseError: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray

that means i am receiving html from url.

how can i receive data in json format from url so that i can parse using jsonObject or jsonArray in android

My android code is

 RequestQueue queue = Volley.newRequestQueue(getContext());

 JsonArrayRequest  request = new JsonArrayRequest(Method.GET, URL,null, new Response.Listener<JSONArray>() {


     @Override
     public void onResponse(JSONArray response ) {


         Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();

         Log.i("My success",""+response.toString());
            try {
                    for (int i = 0; i <= response.length(); i++) {
                    JSONObject silver_rate_values = response.getJSONObject(i);
                    String Silver_Rate = silver_rate_values.getString("silver_rate");
                    Log.v("SilverRate", "silverRate value is" + Silver_Rate);
                }
            }catch (JSONException e) {
                     e.printStackTrace();
                     Log.e("Silver rate", "Json parsing error: " + e.getMessage());
                 }

            }

     }, new Response.ErrorListener() {

     @Override

     public void onErrorResponse(VolleyError error) {


         Toast.makeText(getContext(), "my error :"+error, Toast.LENGTH_LONG).show();

         Log.i("My error",""+error);

     }

 }){


 };



My php code is

    $sql = "SELECT * FROM `user_info`  
"

. "ORDER BY `customer_info`.`updatedTime`  DESC LIMIT 1";


   $result = mysqli_query(  $con,$sql );


   if(! $result ) {
       
   die('Could not get data: ' . mysql_error());

     }



   //print values to screen
   $data = array();
   while ($row = mysqli_fetch_assoc($result)) {
   $a = $row['a'];
   $b = $row['b'];
   $c =  $row['c'];
   $data[] = $row;


    }
   header('Content-type: application/json');
  

   $json=json_encode(array("Values" => $data));

   echo $json;

   ?>
</body>
My php output is
    <html>
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    {"Values":[{"a":"8888","b":"9999","c"7777"}]}   </body>
    </html>

If I remove header from php code then the output is {"Values":[{"a":"8888","b":"9999","c"7777"}]}

Then also I am getting same error from Android side

question from:https://stackoverflow.com/questions/65930175/how-to-get-json-array-not-html-from-url-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just Add this line you can achive result

$jsonfinal = json_encode(json_decode($json), JSON_PRETTY_PRINT);
       echo $jsonfinal ;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...