1

Log shows response like this:

Image 1

I sent a PHP array using JSON encode from server which outputs this:

Image 2

How can I get my array from the unexpected response code? The array is sent as JSON encode.

PHP:

<?php

require "conn.php"; 
class person {
        public $id;
        public $age;
        public $name;
        public $lat;
        public $longi;
        public $email;
        public $image;
        public $number;
    }


$bloodgroup = $_POST["bloodgroup"];
$sql = "Select * from UserDetails where bloodgroup = '$bloodgroup'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
$donorsArray = array();
    while($row = $result->fetch_assoc()) {
        $donor = new person;
        $donor->id = $row["ID"];
        $donor->age = $row["age"];
        $donor->name = $row["Name"];
        $donor->lat = $row["lat"];
        $donor->longi = $row["longi"];
        $donor->email = $row["email"];
        $donor->image = $row["image"];
        $donor->number = $row["number"]; 
        $donorsArray[] = $donor;
    }
} else {
}

echo json_encode(array_values($donorsArray));

?>

Java:

OkHttpClient client = new OkHttpClient();

        RequestBody formBody = new FormBody.Builder()
                .add("bloodgroup",bloodgroup)
                .build();
        Request request = new Request.Builder()
                .url("http://blooddonation.byethost6.com/GetDonorsDetails.php")
                .post(formBody)
                .build();

        try {
            Response response = client.newCall(request).execute();
            String res = response.body().string();

            Log.e("responseaaa",res);

            // Do something with the response.
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("responseaaa","with error "+e.toString());
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
Yousaf Iqbal
  • 85
  • 2
  • 10
  • You again :) I see you listened to my advice in using OkHttp. Good. Now this raw php is ugly, but what can you do. You need to set the header in your response to be `json` like this `header('Content-Type: application/json');` Because now the server is returning html with a json string. After that you can edit the question, so we can help you parse the proper json you get from the server. – Vucko Jun 25 '16 at 19:57
  • @Vucko somthing like this ? Response response =client.newCall(request).execute(); response.header("Content-Type: application/json"); – Yousaf Iqbal Jun 25 '16 at 20:02
  • I'd guess from your SQL injection vulnerability that I could use a `UNION` to read entries from your user table - don't put this live until you have fixed this! – halfer Jun 25 '16 at 20:02
  • No, I mean to set the header in php. – Vucko Jun 25 '16 at 20:02
  • 1
    If you are working on a medical application then it is possible you should consider **getting a security professional to assess your code** before you use it in a production environment. – halfer Jun 25 '16 at 20:03
  • @Vucko i tried but not working i guess the problem is with the server. just look at this link. i guess my issue is same as this one because i am also using free hosting http://stackoverflow.com/questions/32409655/getting-html-response-instead-of-json-in-android what u say ? – Yousaf Iqbal Jun 25 '16 at 21:04
  • @halfer okay sir :-) – Yousaf Iqbal Jun 25 '16 at 21:04
  • @halfer how can i prolong the connection timeout in httpok ? setConnectTimeout method is not resolved by okhttpclient object :-( – Yousaf Iqbal Jul 27 '16 at 02:54
  • I'm not a Java programmer, so am unlikely to be able to help - maybe @Vucko can shed some light? – halfer Jul 27 '16 at 07:47

0 Answers0