2
I am trying to develop an application on android 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.
He says the error is in the Hindground method of line 134, and what’s on it is:
int success = json.getInt(TAG_SUCCESS);
My Class that managed this consultation:
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 searched and do not think the error is coming from my php script, so much so that the error points out at the time of conversation, someone could help me?
but how do I apply this to my @ramaral code? see the rest of the code after this int? if I put this error in the rest of the method
– Jeiferson
That’s why I told you to review the origin of the json, because I don’t think it’s supposed to
success
be null. However you can overcome this by doing instead of,if (success == 1)
, doif (success == "1")
– ramaral
same thing @ramaral, has not changed anything, it is noticed that it returns org.json.Jsonexception: No value for Success, means that no value has been returned, which can be?
– Jeiferson
when I try to search my browser it returns this: {"products":[{"pid":"1","name":"iPhone","price":"4500.00","created_at":"2015-02-18 20:02:20","updated_at":"""},{"pid":"2","name":"Motorola Moto G 2014","price":"800.00","created_at":"2015-02-19 18:56:46","updated_at":"""},{"pid":"4","name":"LG G3","price":"1200.00","created_at":"2015-02-19 21:49:25","updated_at":""}],"Scessuc":1} / at the end returns Success:1, is that what is feeding the variable right? is that my application is failing to catch her?
– Jeiferson
See on log produced by this line:
Log.d("All Products: ", json.toString());
if you are"success":1
.– ramaral
this empty that line
– Jeiferson
That means you’re not returning anything on the line
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
. The URL may be wrong!– ramaral
ramaral, I made this query in my browser and returned all the data, so the url is right... will the error is in php script?
– Jeiferson
Is Webservice on your machine? You are running the program on the emulator or on a mobile/tablet?
– ramaral
This on localhost, I’m using Wamp Server
– Jeiferson