I'm trying to develop an android application that consumes a Web-Service but whenever I try to run a query in the database it returns me this error:
Error parsing data org.json.JSONException: Value
In my LogCat:
02-22 15:37:32.250: I/Choreographer(1446): Skipped 232 frames! The application may be doing too much work on its main thread.
02-22 15:37:36.712: I/Choreographer(1446): Skipped 46 frames! The application may be doing too much work on its main thread.
02-22 15:37:37.959: I/Choreographer(1446): Skipped 112 frames! The application may be doing too much work on its main thread.
02-22 15:37:38.611: E/JSON Parser(1446): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
02-22 15:37:38.612: D/All Products:(1446): {}
02-22 15:37:38.612: W/System.err(1446): org.json.JSONException: No value for success
02-22 15:37:38.613: W/System.err(1446): at org.json.JSONObject.get(JSONObject.java:389)
02-22 15:37:38.613: W/System.err(1446): at org.json.JSONObject.getInt(JSONObject.java:478)
02-22 15:37:38.614: W/System.err(1446): at br.com.products.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:134)
02-22 15:37:38.614: W/System.err(1446): at br.com.products.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-22 15:37:38.684: W/System.err(1446): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-22 15:37:38.685: W/System.err(1446): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-22 15:37:38.693: W/System.err(1446): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-22 15:37:38.693: W/System.err(1446): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-22 15:37:38.693: W/System.err(1446): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-22 15:37:38.694: W/System.err(1446): at java.lang.Thread.run(Thread.java:818)
02-22 15:37:39.680: I/Choreographer(1446): Skipped 184 frames! The application may be doing too much work on its main thread.
02-22 15:37:41.397: I/Choreographer(1446): Skipped 184 frames! The application may be doing too much work on its main thread.
It says that the error is in theInBackground method of line 134, and what it has in it is:
int success = json.getInt(TAG_SUCCESS);
My Class that granted this query:
AllProductsActivity.java
package br.com.products;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AllProductsActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://XXX.XXX.X.X/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray products = new JSONArray();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
I have tried and I do not think the error is coming from my php script, so much that the error points at the time of the conversion, could anyone help me?