i am currently trying a tutorial to connect an android app with a server (link: https://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/).
However, i have difficulties with an AsyncTask which is supposed to load the elements of a database. It causes the app to crash and leaves no error logs in the logcat (also tried "no filter"). I surrounded the execute call with a try catch block as well, but the app still crashes. Any ideas what is causing the error?
here are extracts from the code:
public class AllProductsActivity extends ListActivity {
private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php";
...
#Override
public void onCreate(Bundle savedInstanceState) {
try {
new LoadAllProducts().execute();
}catch (Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
...
/**
* 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
* */
#Override
protected String doInBackground(String... args) {
// Building Parameters
List<Pair> params = new ArrayList<Pair>();
// 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(),
AddNewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* 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);
}
});
}
}
The problem is not with your code implementation. I can see webservice url in your code as
private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php";
The problem is with this web service. That is no longer working. I tested the webservice as you mentioned the link of the tutorial over postman, that is not working any more. Hope you get my point. Feel free to ask me if you have any confusions.
Related
I wrote an Android app to manage a MySQL database, and I connect/add/delete rows with some PHP classes that are called by a HttpRequest in an AsyncTask in the background. There is just one simple thing I want, like:
Home screen -> Add screen -- Press add button --> Home screen
But when I add a row into the database, the listview won't refresh. But if I restart the app everything is getting recreated, and my listview displays the added row.
To get the information from the database, I use this code:
class LoadAllBooks extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoadAllActivity.this);
pDialog.setMessage("Loading authors. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Getting all books from a 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_books, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All Authors: ", 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_BOOKS);
// Looping through all products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each JSON item in variable
String author = c.getString(TAG_AUTHOR);
// Creating a new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Adding each child node to HashMap key => value.
// map.put(TAG_ID, id);
map.put(TAG_AUTHOR, author);
// Adding HashList to ArrayList
booksList.add(map);
}
} else {
// No products found
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing a background task, dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// Dismiss the dialog after getting all products
pDialog.dismiss();
// Updating UI from the background thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
SimpleAdapter adapter = new SimpleAdapter(
LoadAllActivity.this, booksList, R.layout.menu_row,
new String[] { TAG_ID, TAG_AUTHOR }, new int[] {
R.id.id, R.id.book });
// Updating listview
setListAdapter(adapter);
}
});
}
I know there is a way to update it when updating the booksList, but I tried that in many different ways, and it's quite not the simpliest way I think.
The way I'd like to solve it is just to restart the main activity, to recall this onCreate(Bundle ...) method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
booksList = new ArrayList<HashMap<String, String>>();
// Loading products in the background thread
new LoadAllBooks().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 pos,
long id) {
String author = ((TextView) view.findViewById(R.id.book))
.getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AuthorTitlesActivity.class);
// Sending pid to next activity
in.putExtra(TAG_AUTHOR, author);
// Starting a new activity and expecting some response back.
startActivityForResult(in, 100);
}
});
}
Is there a way to realize this solution?
I have made a program in which I am using PHP to encode data as JSON. My program is working fine, and is not showing any errors or problems; I am getting what I want but I am facing very small issue.
Here, I am not able to show an item's image in ListView but whenever I click on one of the item rows to view full item detail, I get the image of selected item. It means I should be getting the image in ListView, but I'm unable to show that, so could someone please help to sort out this mistake?
public class AlbumsActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> albumsList;
// albums JSONArray
JSONArray albums = null;
String imagepath;
// albums JSON url
private static final String URL_ALBUMS = "MY URL";
// ALL JSON node names
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_IMAGEPATH = "imagepath";
private static final String TAG_SONGS_COUNT = "songs_count";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
albumsList = new ArrayList<HashMap<String, String>>();
// Loading Albums JSON in Background Thread
new LoadAlbums().execute();
// get listview
ListView lv = getListView();
/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// on selecting a single album
// TrackListActivity will be launched to show tracks inside the album
Intent i = new Intent(getApplicationContext(), TrackListActivity.class);
// send album id to tracklist activity to get list of songs under that album
String album_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString();
i.putExtra("album_id", album_id);
startActivity(i);
}
});
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AlbumsActivity.this);
pDialog.setMessage("Listing Albums ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Albums JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Albums JSON: ", "> " + json);
try {
albums = new JSONArray(json);
if (albums != null) {
// looping through All albums
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
// Storing each json item values in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String description = c.getString(TAG_DESCRIPTION);
String imageurl = c.getString(TAG_IMAGEPATH);
String songs_count = c.getString(TAG_SONGS_COUNT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_IMAGEPATH,imageurl);
map.put(TAG_SONGS_COUNT, songs_count);
// adding HashList to ArrayList
albumsList.add(map);
}
}else{
Log.d("Albums: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AlbumsActivity.this, albumsList,
R.layout.list_item_albums, new String[]
{
TAG_ID, TAG_NAME, TAG_DESCRIPTION, TAG_IMAGEPATH, TAG_SONGS_COUNT
},
new int[]
{
R.id.album_id, R.id.album_name, R.id.album_description, R.id.list_image, R.id.songs_count
}
);
// updating listview
setListAdapter(adapter);
}
});
}
}
You have to first download the image and then show it in list view. You can not directly pass the image URL, it will not work.
Change your current code as and also remove runOnUiThread() from onPostExecute method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
albumsList = new ArrayList<HashMap<String, String>>();
// Loading Albums JSON in Background Thread
new LoadAlbums().execute();
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String,
ArrayList<HashMap<String, String>>> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AlbumsActivity.this);
pDialog.setMessage("Listing Albums ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Albums JSON
* */
protected ArrayList<HashMap<String, String>>
doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Albums JSON: ", "> " + json);
try {
albums = new JSONArray(json);
if (albums != null) {
// looping through All albums
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
// Storing each json item values in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String description = c.getString(TAG_DESCRIPTION);
String imageurl = c.getString(TAG_IMAGEPATH);
String songs_count = c.getString(TAG_SONGS_COUNT);
// creating new HashMap
HashMap<String, String> map = new
HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_IMAGEPATH,imageurl);
map.put(TAG_SONGS_COUNT, songs_count);
// adding HashList to ArrayList
albumsList.add(map);
}
}else{
Log.d("Albums: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return albumsList;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(ArrayList<HashMap<String,
String>> file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// get listview
ListView lv = getListView();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AlbumsActivity.this, file_url,
R.layout.list_item_albums, new String[]
{
TAG_ID, TAG_NAME, TAG_DESCRIPTION,
TAG_IMAGEPATH, TAG_SONGS_COUNT
},
new int[]
{
R.id.album_id,
R.id.album_name, R.id.album_description, R.id.list_image, R.id.songs_count
}
);
// updating listview
lv.setListAdapter(adapter);
/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
lv.setOnItemClickListener(new android.widget.
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// on selecting a single album
Intent i = new Intent(getApplicationContext(),
TrackListActivity.class);
// send album id to tracklist activity to get list of songs
//under that album
String album_id = ((TextView) view.
findViewById(R.id.album_id)).getText().toString();
i.putExtra("album_id", album_id);
startActivity(i);
}
});
}
}
NOTE : if you are given direct web URL in ImageView then this will never work because you will need first download image from Web then set it to ImageView src.
see this post how we take image from web server
How to load an ImageView by URL in Android?
It means that you want to customize your list view with image and text. For that (what i did in my case), get the image url and text in two arrays. then set the adapter of the list view by passing context, both arrays.
For setting up images, you need to import the library (Either image url helper or Universe image loader). Both can help you well but i will recommend you for the image url helper library because it is easy to use.
Try this in your Adapter, It might help you:
String imageURL = "htp://domain/some-path/kdhfbdskf.jpg";
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(imageURL).openStream();
bmp = BitmapFactory.decodeStream(in);
}
catch (IOException e) {
e.printStackTrace();
}
if (bmp != null) {
holder.UserImageView.setImageBitmap(bmp);
} else {
holder.UserImageView.setImageResource(R.drawable.ic_launcher);
}
remov the runOnUiThread() from the post Execute and you do not run UI component into thread.. simply set images...
the problem that im having is kind of weird the printstack of the JSON is correct displaying all the element in the table the way that it should the same as for the method String doInBackground(String... args) the problem is in the postExecute method that is displaying the same element in all the element of the listview "the last element in the row of the table to be exact can someone tell me what i did wrong " thank you for your time bellow you will find the class that im talking about thank you
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ProDentActivity.this);
pDialog.setMessage("Loading Balance. 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.getJSONFromUrl(balanceURL, "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
amount = json.getJSONArray(TAG_BALANCE);
// looping through All Products
for (int i = 0; i < amount.length(); i++) {
JSONObject c = amount.getJSONObject(i);
// Storing each json item in variable
String amount = c.getString(TAG_AMOUNT);
String createdat = c.getString(TAG_CREATEDAT);
//String userid = c.getString(TAG_USERID);
// adding each child node to HashMap key => value
map.put(TAG_AMOUNT, amount);
map.put(TAG_CREATEDAT, createdat);
//map.put(TAG_USERID , userid);
// adding HashList to ArrayList
amountlist.add(map);
}
} else {
// no balance found
// Launch Add New balance Activity
Intent i = new Intent(getApplicationContext(),
ProDentActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
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() {
ListAdapter adapter = new SimpleAdapter(
ProDentActivity.this, amountlist,
R.layout.list_item, new String[] { TAG_AMOUNT,TAG_CREATEDAT},
new int[] { R.id.amount, R.id.createdat });
// updating listview
setListAdapter(adapter);
}
});
}
}
JSON:
{"balance":[{"amount":"50000","created_at":"2012-12-15 02:39:13"},{"amount":"50000","created_at":"2012-12-16 15:29:03"},{"amount":"30000","created_at":"2012-12-17 19:38:07"}],"success":1}
...and the method returns
12-18 02:29:05.797: D/All Products:(885): {"success":1,"balance":[{"amount":"50000","created_at":"2012-12-15 02:39:13"},{"amount":"50000","created_at":"2012-12-16 15:29:03"},{"amount":"30000","created_at":"2012-12-17 19:38:07"}]}
problem fixed i found what i have been doing wrong. the problem was that i didn't define my hash map in the method protected String doInBackground(String... args)
i was defining it in the listactivity anw thank you for your time and help
bellow code to be replaced with the old one
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
amount = json.getJSONArray(TAG_BALANCE);
// looping through All Products
for (int i = 0; i < amount.length(); i++) {
JSONObject c = amount.getJSONObject(i);
// Storing each json item in variable
String amount = c.getString(TAG_AMOUNT);
String createdat = c.getString(TAG_CREATEDAT);
//String userid = c.getString(TAG_USERID);
// adding each child node to HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_AMOUNT, amount);
map.put(TAG_CREATEDAT, createdat);
//map.put(TAG_USERID , userid);
// adding HashList to ArrayList
amountlist.add(map);
}
} else {
// no balance found
// Launch Add New balance Activity
Intent i = new Intent(getApplicationContext(),
ProDentActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
I have at least three activities in my app right now that use an AsyncTask to return JSON results into an ListView. I've started work on the app, but another person will take over development as soon as he gets the basics down, so I want to try and make things as easy to use as possible. This means that I'm trying to turn as much repeatable code into callable functions as possible, so instead of needing to copy/paste/reuse 30-40 lines of code each time they need query a webservice, they can just pass in parameters to a function.
Currently, I have the following in an activity that pulls a list of gym classes from a mysql database via a php webservice:
class LoadAllClasses extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(Checkin.this);
// pDialog.setMessage("Loading products. Please wait...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(false);
// pDialog.show();
}
/**
* getting All products from url
* */
#Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getclasses_tag));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL,
"POST", params);
// Check your log cat for JSON response
Log.d("CheckinDialog", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes2 = json.getJSONArray(TAG_CLASSES);
// looping through All Classes
for (int i = 0; i < classes2.length(); i++) {
JSONObject c = classes2.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
//String day = c.getString(TAG_DAY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
//map.put(TAG_DAY, day);
// adding HashList to ArrayList
allclasseslist.add(map);
Log.d("map: ", map.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
runOnUiThread(new Runnable() {
#Override
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(CheckinDialog.this,
allclasseslist, R.layout.checkin_item,
new String[] { TAG_CLASSID, TAG_CLASSNAME },
new int[] { R.id.pid, R.id.name });
setListAdapter(adapter);
}
});
//pDialog.dismiss();
// updating UI from Background Thread
}
}
I'd like to move this to another class that I have, called "WebServiceTasks", so that I can call something like this in the activity's OnCreate():
allclasseslist = new ArrayList<HashMap<String, String>>();
allclasseslist = new WebServiceTasks.LoadAllClasses().get();
adapter = new SimpleAdapter(CheckinDialog.this,
allclasseslist, R.layout.checkin_item,
new String[] { TAG_CLASSID, TAG_CLASSNAME },
new int[] { R.id.pid, R.id.name });
setListAdapter(adapter);
While I've tried this, I get a number of errors related to either defining the asyncTask wrong, or other things not matching up.
Here is what I've tried putting in my "WebServiceTasks" class:
public static class LoadAllClasses extends
AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> allclasseslist;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CLASSES = "classes";
private static final String TAG_CLASSID = "id";
private static final String TAG_CLASSNAME = "class";
private static final String getclasses_tag = "getclasses";
JSONArray classes2 = null;
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(Checkin.this);
// pDialog.setMessage("Loading products. Please wait...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(false);
// pDialog.show();
}
/**
* getting All classes from url
* */
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getclasses_tag));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL,
"POST", params);
// Check your log cat for JSON response
Log.d("CheckinDialog", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes2 = json.getJSONArray(TAG_CLASSES);
// looping through All Classes
for (int i = 0; i < classes2.length(); i++) {
JSONObject c = classes2.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
//String day = c.getString(TAG_DAY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
//map.put(TAG_DAY, day);
// adding HashList to ArrayList
allclasseslist.add(map);
Log.d("map: ", map.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return allclasseslist;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> allclasses) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
}
}
Is this possible, and if so, what am I doing wrong?
Well, you are trying to use get() method of AsyncTask which is very expensive because it blocks the UI until your onPostExecute() is completed. I would insist you to fire a BroadCastReceiver in onPostExecute() to update your UI or create and Interface and pass the result to your Activity using that interface in your onPostExecute(). I had just created a small demo for using BroadCastReceiver and Interface for passing result from onPostExecute() to your Activity. You can find a demo source from my github here.
You Can Create A Bean class to store all the values from async task so that your can receive it to another class my friend
For anyone trying to replicate this, Here is how I solved this, using Lalit and Samir's examples:
In my Activity:
public class CheckinDialog extends ListActivity implements
AsyncTaskCompleteListener {
WebServiceTasks.LoadAllClasses objAsyncTask = new WebServiceTasks.LoadAllClasses(
this);
objAsyncTask.execute();
#Override
public void onTaskComplete(ArrayList<HashMap<String, String>> allclasseslist) {
// TODO Auto-generated method stub
adapter = new SimpleAdapter(CheckinDialog.this, allclasseslist,
R.layout.checkin_item, new String[] { TAG_CLASSID,
TAG_CLASSNAME }, new int[] { R.id.pid, R.id.name });
setListAdapter(adapter);
Log.d("OnTaskComplete", "taskcomplete");
}
In an interface called "AsyncTaskCompleteListener":
public interface AsyncTaskCompleteListener {
void onTaskComplete(ArrayList<HashMap<String, String>> allclasseslist);
}
And the seperate WebServiceTasks Class:
public static class LoadAllClasses extends
AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
JSONParser jParser = new JSONParser();
private final AsyncTaskCompleteListener callback;
private final Activity activity;
public LoadAllClasses(Activity act) {
this.activity = act;
this.callback = (AsyncTaskCompleteListener) act;
}
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CLASSES = "classes";
private static final String TAG_CLASSID = "id";
private static final String TAG_CLASSNAME = "class";
private static final String getclasses_tag = "getclasses";
JSONArray classes2 = null;
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All classes from url
* */
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... args) {
// Building Parameters
ArrayList<HashMap<String, String>> allclasseslist = null;
allclasseslist = new ArrayList<HashMap<String, String>>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getclasses_tag));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL,
"POST", params);
// Check your log cat for JSON response
Log.d("CheckinDialog", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes2 = json.getJSONArray(TAG_CLASSES);
Log.d("JSONArray", json.getJSONArray(TAG_CLASSES)
.toString());
// looping through All Classes
for (int i = 0; i < classes2.length(); i++) {
JSONObject c = classes2.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
//String day = c.getString(TAG_DAY);
// creating new HashMap
final HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
//map.put(TAG_DAY, day);
// adding HashList to ArrayList
allclasseslist.add(map);
//Log.d("map: ", map.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return allclasseslist;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> allclasseslist) {
super.onPostExecute(allclasseslist);
// dismiss the dialog after getting all classes
callback.onTaskComplete(allclasseslist);
}
}
Thanks to all for the quick help. This ended up saving over 120 lines of repeating code in each activity that used this code.
I have a JSON string which is generated by a php file. The problem is that the JSON string is not appearing on the textview of android. can anyone explain why?
public class AllUsersActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> usersList;
// url to get all users list
private static String url_all_users = "http://10.0.2.2/android_connect/get_all_users.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_USERS = "users";
private static final String TAG_UID = "UserID";
private static final String TAG_FIRSTNAME = "FirstName";
// users JSONArray
JSONArray users = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_users);
// Hashmap for ListView
usersList = new ArrayList<HashMap<String, String>>();
// Loading users in Background Thread
new LoadAllusers().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String uid = ((TextView) view.findViewById(R.id.uid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
UserDetailsActivity.class);
// sending uid to next activity
in.putExtra(TAG_UID, uid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllusers extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllUsersActivity.this);
pDialog.setMessage("Loading users. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All users 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_users, "GET", params);
// Check your log cat for JSON reponse
Log.d("All users: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// users found
// Getting Array of users
users = json.getJSONArray(TAG_USERS);
// looping through All users
for (int i = 0; i < users.length(); i++) {
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_UID);
String name = c.getString(TAG_FIRSTNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_UID, id);
map.put(TAG_FIRSTNAME, name);
// adding HashList to ArrayList
usersList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all users
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllUsersActivity.this, usersList,
R.layout.list_item, new String[] { TAG_UID,
TAG_FIRSTNAME},
new int[] { R.id.uid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
this is the JSON string from the PHP file
{"Users":[{"0":[],"UserID":"1","FirstName":"lalawee","Email":"12345","Password":null},{"0":[],"UserID":"2","FirstName":"shadowblade721","Email":"12345","Password":null,"1":[]},{"0":[],"UserID":"3","FirstName":"dingdang","Email":"12345","Password":null,"1":[],"2":[]},{"0":[],"UserID":"4","FirstName":"solidsnake0328","Email":"12345","Password":null,"1":[],"2":[],"3":[]}],"success":1}
I've found the error.
It is caused by case sensitiveness.
users --> Users
Mistake on my part. Sorry.
Thanks for trying to help me!