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