-1

I am working on a piece of code where on the click of an item in the spinner,corresponding case should be called in the php and appropriate json should be returned. The code works fine when I hard code a value in the php file, but when i pass it from the spinner, the errors fires

Below is the relevant code snippet:

protected String doInBackground(String... params) {
    String paramDays = params[0];
     String sel_Day = spn_getDays.getSelectedItem().toString();
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("DAYS", sel_Day));
        try {
        @SuppressWarnings({ "deprecation", "resource" })
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://192.168.10.63/try.php");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is =   entity.getContent();

         } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }
  arraylist = new ArrayList<DataItem>();
    // Retrieve JSON Objects from the given URL address
    jsonobject = JSONfunctions.getJSONfromURL("http://192.168.10.63/try.php");
    try {
        // Locate the array name in JSON
        jsonarray = jsonobject.getJSONArray("images");
        for (int i = 0; i < jsonarray.length(); i++) {
            DataItem di = new DataItem();
            jsonobject = jsonarray.getJSONObject(i);
            // Retrive JSON Objects
            Log.e("",jsonobject.getString("image_name"));
            di.setPlace_image(jsonobject.getString("image_name"));
            arraylist.add(di);

        }
    } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
return "success";
}

and the PHP code:

<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));
    $days = $_POST['DAYS'];
    //$days = 1;
switch ($days){
        case 1:

//fetch table rows from mysql db
$sql = "select image from one";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$imageArray = array();
$imageArray["images"] = array();

while($row =mysqli_fetch_array($result))
{
    $tmp = array();
    //$tmp["id"] = $row["id"];
    $tmp["image_name"] = $row["image"];
    array_push($imageArray["images"], $tmp);

   // $imageArray[] = $row;
}
 json_encode($imageArray);
break;

case 2:
 //fetch table rows from mysql db
$sql = "select image from two";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$imageArray = array();
$imageArray["images"] = array();

while($row =mysqli_fetch_array($result))
{
    $tmp = array();
    //$tmp["id"] = $row["id"];
    $tmp["image_name"] = $row["image"];
    array_push($imageArray["images"], $tmp);

   // $imageArray[] = $row;
}
 json_encode($imageArray);
break;
default:
echo "nothing";

}

//close the db connection
mysqli_close($connection);
?>

I have looked through many SO answers for the same doubt, but couldn't solve my error, Please help! I am new to the working of json.

Here is the logcat o/p:

04-20 11:52:27.228: E/log_tag(725): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
04-20 11:52:27.298: E/AndroidRuntime(725): FATAL EXCEPTION: AsyncTask #1
04-20 11:52:27.298: E/AndroidRuntime(725): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 11:52:27.298: E/AndroidRuntime(725):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 11:52:27.298: E/AndroidRuntime(725):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.lang.Thread.run(Thread.java:856)
04-20 11:52:27.298: E/AndroidRuntime(725): Caused by: java.lang.NullPointerException
04-20 11:52:27.298: E/AndroidRuntime(725):  at com.example.explore.ImageActivity$1DaysSent.doInBackground(ImageActivity.java:95)
04-20 11:52:27.298: E/AndroidRuntime(725):  at com.example.explore.ImageActivity$1DaysSent.doInBackground(ImageActivity.java:1)
04-20 11:52:27.298: E/AndroidRuntime(725):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-20 11:52:27.298: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 11:52:27.298: E/AndroidRuntime(725):  ... 5 more
J.K
  • 1,382
  • 1
  • 11
  • 27
  • you can change the php code like a previous stackoverflow answer please check here is the link [answer](http://stackoverflow.com/questions/383631/json-encode-mysql-results) – anuraj Apr 20 '16 at 07:08
  • possibly duplicate question http://stackoverflow.com/questions/23111640/value-br-of-type-java-lang-string-cannot-be-converted-to-jsonobject-on-android – Sunil Sunny Apr 20 '16 at 07:22

2 Answers2

0

Every time I've seen this it's been because PHP was set up to output errors and was doing so in HTML format on the same stream that got returned to the client, resulting in HTML being mixed into the JSON. Try using a browser, a CURL request, or something similar to see what's actually being sent as the response. There's probably error output in there.

kungphu
  • 4,592
  • 3
  • 28
  • 37
0

Spinner valur not reaching the php file. so $days doesn't get the value. because of which your default switch case also returns a string.

even in error/default switch json_encode the error like blank array [] or object {}

for getting post data try this :-

$postdata = file_get_contents("php://input");
if (isset($postdata)) {
  $request = json_decode($postdata);
  $days= $request->DAYS;

}
UzUmAkI_NaRuTo
  • 565
  • 3
  • 8
  • 20