Like title says, my variable is not updated on AsyncTask onPostExecute()
Here is the code
public class Search extends AppCompatActivity {
ArrayList<paire> Sectors = new ArrayList<paire>();//paire is my proper class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Rechercher rech = new Rechercher();
rech.execute();
//PRINTING SIZE OF Sectors HERE TELLS ME EXACTLY THE SIZE OF 0
/*
*
*
BLA BLA BLA BLA
*
*
*/
}
public class Rechercher extends AsyncTask<String, Void, ArrayList<paire>>{
#Override
protected ArrayList<paire>doInBackground(String... strings) {
/*
*
Process of creating the arrayList myArray
*
*/
onPostExecute(myArray);//I put this desesperatly xD it doesn't change anything
return myArray;
}
protected void onPostExecute(ArrayList<paire>myArray) {
for (int u = 0; u < myArray.size(); u ++){
paire r = myArray.get(u);
Sectors.add(r);
}
//PRINTING SIZE OF Sectors HERE TELLS ME EXACTLY THE SIZE OF myArray
}
}
}
Well myArray is created from data gotten from dataBase (HTTP connection, JSON result ...) and the result is a very acceptable JSON output; there's no problem here, it's just that Sectors is not updated if I try to use it on Main.
I don't know if I really understand the onPostExecute; or there's a problem !
Thank you
onPostExcecute will call automatically just return your array in doBackground and pass ArrayList instead of void in AsyncTask<String, Void, ArrayList<paire>> and change return type of doInBackground to ArrayList<paire>
try this:
public class Rechercher extends AsyncTask<String, Void, ArrayList<paire>>{
#Override
protected ArrayList<paire> doInBackground(String... strings) {
/*
*
Process of creating the arrayList myArray
*
*/
//onPostExecute(myArray); // remove this line no need to add . this will call automatically.
return myArray;
}
protected void onPostExecute(ArrayList<paire> myArray) {
for (int u = 0; u < s.size(); u ++){
Sectors.set(u, s.get(u));
}
//PRINTING SIZE OF Sectors HERE TELLS ME EXACTLY THE SIZE OF myArray
}
}
hope this help.
Is this building without errors? I dont believe that you can call onPostExecute from doInBackground and I dont see anywhere in the code where there is a variable called "s" but s.size() is what is controlling the number of times the for loop iterates. So in the current code, Sectors.set would never be called.
you should read careful about asyncTask again.
Modify you asynTask as below
public class Rechercher extends AsyncTask<String, Void, ArrayList<paire>>{
#Override
protected Void doInBackground(String... strings) {
/*
*
Process of creating the arrayList myArray
*
*/
/* don't need to call this function, asynctask will call it automatically
onPostExecute(myArray);
*/
return myArray;
}
protected void onPostExecute(ArrayList<paire>myArray) {
if (Selector.size() > 0) {
Selector.clear();
}
for (int u = 0; u < s.size(); u ++){
Sectors.add(s.get(u));
}
//PRINTING SIZE OF Sectors HERE TELLS ME EXACTLY THE SIZE OF myArray
}
}
package caci.elmouchir;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
import android.util.Log;
import android.widget.*;
import android.widget.Spinner;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Search extends AppCompatActivity {
private void setSectors(String[] lesSec){
Spinner sec = (Spinner) findViewById(R.id.spinnerSecteur);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lesSec);
sec.setAdapter(adapter);
sec.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
ArrayList<paire> Sectors = new ArrayList<paire>();
ArrayList<paire> Branchs = new ArrayList<paire>();
ArrayList<paire> SBranchs = new ArrayList<paire>();
ArrayList<paire> Activities = new ArrayList<paire>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
String Code = "X.XX.XX.XXX";
Rechercher rech = new Rechercher();
rech.execute();
Log.e("log_tag", Integer.toString(Sectors.size()));
//Log.e("log_tag", Integer.toString(Sectors.size()));
Button search = (Button)findViewById(R.id.buttonSearch);
search.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
EditText motcle = (EditText)findViewById(R.id.editMotCle);
if (motcle.getText().toString().isEmpty())
motcle.setError("Entrez un mot clé");
}
});
/*
*
*
*
*
C O N N E X I O N A L A B A S E D E D O N N E E S
*
*
*
*
*/
}
public class Rechercher extends AsyncTask<String, Void, ArrayList<paire>>{
#Override
protected ArrayList<paire> doInBackground(String... strings) {
String result = "";
ArrayList<paire> s = new ArrayList<paire>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.145/test/getActivities.php");
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
//Log.e("log_tag", result);
try{
JSONArray jArray = new JSONArray(result);
ArrayList<paire> b = new ArrayList<paire>();
ArrayList<paire> sous = new ArrayList<paire>();
ArrayList<paire> a = new ArrayList<paire>();
for(int i=0;i<jArray.length();i++){
JSONObject json_data = (JSONObject)jArray.getJSONObject(i);
if (json_data.getString("friendly_url").toString().length() == 2){
paire p = new paire(json_data.getString("title").toString(),json_data.getString("friendly_url").toString());
s.add(p);
}
else if(json_data.getString("friendly_url").toString().length() == 3){
b.add(new paire(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}
else if(json_data.getString("friendly_url").toString().length() == 4){
sous.add(new paire(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}
else{
a.add(new paire(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}}
//Log.e("log_tag", Integer.toString(s.size()));
// onPostExecute(s, b, sous, a);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
return s;
}
protected void onPostExecute(ArrayList<paire>s) {
/*Sectors = s;
Branchs = b;
SBranchs = sous;
Activities = a;*/
for (int u = 0; u < s.size(); u ++){
paire r = s.get(u);
Sectors.add(r);
}
Log.e("log_tag", "loool: "+Integer.toString(Sectors.size()));
}
}
public class paire{
String title;
String url;
paire(String p, String u){
this.title = p;
this.url = u;
}
String getTitle(){
return this.title;
}
String getUrl(){
return this.url;
}
}
}
Related
I am accessing a mysql database table to show the tank level .I need to display the value in an image view using a rectangle.Each time value of the level changes I need to change the height of the rectangle.How is it possible in android.In the below code container 1 and container 2 is the variable which stores the level data.How to access this data and change the height of the image view.
package com.example.tg.mysmartcontainer;
import android.os.AsyncTask; import android.os.Bundle; import
android.support.v7.app.ActionBarActivity; import
android.widget.ListAdapter; import android.widget.ListView; import
android.widget.SimpleAdapter; import android.os.Handler; import
org.apache.http.HttpEntity; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost; import
org.apache.http.impl.client.DefaultHttpClient; import
org.apache.http.params.BasicHttpParams; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONObject;
import java.io.BufferedReader; import java.io.InputStream; import
java.io.InputStreamReader; import java.util.ArrayList; import
java.util.HashMap;
public class MainActivity extends ActionBarActivity {
String myJSON;
Handler mHandler;
private static final String TAG_RESULTS = "result";
private static final String TAG_timeStamp = "timeStamp";
private static final String TAG_container1 = "container1";
private static final String TAG_container2 = "container2";
JSONArray container = null;
ArrayList<HashMap<String, String>> ContainerList;
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
ContainerList = new ArrayList<HashMap<String, String>>();
final Handler handler = new Handler();
Runnable refresh = new Runnable() {
#Override
public void run() {
getData();
handler.postDelayed(this, 0);
}
};
handler.postDelayed(refresh, 0);
//getData();
}
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://smartgrocer.000webhostapp.com/get_data.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (Exception squish) {
}
}
return result;
}
#Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
container = jsonObj.getJSONArray(TAG_RESULTS);
if(ContainerList!=null&&ContainerList.size()>0)
ContainerList.clear();
for (int i = 0; i < container.length(); i++) {
JSONObject c = container.getJSONObject(i);
String timeStamp = c.getString(TAG_timeStamp);
String container1 = c.getString(TAG_container1);
String container2 = c.getString(TAG_container2);
HashMap<String, String> container = new HashMap<String, String>();
container.put(TAG_timeStamp, timeStamp);
container.put(TAG_container1, container1);
container.put(TAG_container2, container2);
ContainerList.add(container);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, ContainerList, R.layout.list_item,
new String[]{TAG_timeStamp, TAG_container1, TAG_container2},
new int[]{R.id.timeStamp, R.id.container1, R.id.container2}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
to set the height of the ImageView:
image_view.getLayoutParams().height = 20;
Hope this helps.
Important. If you're setting the height after the layout has already been 'laid out', make sure you also call:
image_view.requestLayout()
Change the height of the imageView according to the current value :
tankImageView.setHeight((int)(currentValue / maxValue));
I'm New to android development and I don't understand clearly how to create a appwidget for application that parse JSON Data and display in list.
I solved my problem using this link (https://laaptu.wordpress.com/2013/07/19/android-app-widget-with-listview/).
It has Series of Tutorials
(1.app widget with listview
2.populate app widget listview with data from web
3.download images and show on imageview of appwidget with listview
4.setting update interval on appwidget with listview
5.how to make appwidget update work after phone reboot)
To Use Simple JSON URL to fetch images and texts, i made the following changes in RemoteFetchService.java from third tutorial,
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.example.mk.widgets.data.DatabaseManager;
import com.example.mk.widgets.data.FileManager;
public class RemoteFetchService extends Service {
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
JSONObject jsonobject;
JSONArray jsonarray;
AQuery aquery;
private String remoteJsonUrl = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
public static ArrayList<ListItem> listItemList;
private int count = 0;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
/*
* Retrieve appwidget id from intent it is needed to update widget later
* initialize our AQuery class
*/
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID))
appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
aquery = new AQuery(getBaseContext());
new DownloadJSON().execute();
return super.onStartCommand(intent, flags, startId);
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// Create an array
listItemList = new ArrayList<ListItem>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions.getJSONfromURL(remoteJsonUrl);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("actors");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
ListItem listItem = new ListItem();
listItem.heading = jsonobject.getString("name");
listItem.content = jsonobject.getString("country");
listItem.imageUrl = jsonobject.getString("image");
listItemList.add(listItem);
}
storeListItem();
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
}
/**
* Instead of using static ArrayList as we have used before,no we rely upon
* data stored on database so saving the fetched json file content into
* database and at same time downloading the image from web as well
*/
private void storeListItem() {
DatabaseManager dbManager = DatabaseManager.INSTANCE;
dbManager.init(getBaseContext());
dbManager.storeListItems(appWidgetId, listItemList);
int length = listItemList.size();
for (int i = 0; i < length; i++) {
ListItem listItem = listItemList.get(i);
final int index = i;
aquery.ajax(listItem.imageUrl, Bitmap.class,new AjaxCallback<Bitmap>() {
#Override
public void callback(String url, Bitmap bitmap, AjaxStatus status) {
super.callback(url, bitmap, status);
storeBitmap(index, bitmap);
};
});
}
}
/**
* Saving the downloaded images into file and after all the download of
* images be complete begin to populate widget as done previously
*/
private void storeBitmap(int index, Bitmap bitmap) {
FileManager.INSTANCE.storeBitmap(appWidgetId, bitmap,
listItemList.get(index).heading, getBaseContext());
count++;
Log.i("count",String.valueOf(count) + "::"+ Integer.toString(listItemList.size()));
if (count == listItemList.size()) {
count = 0;
populateWidget();
}
}
/**
* Method which sends broadcast to WidgetProvider so that widget is notified
* to do necessary action and here action == WidgetProvider.DATA_FETCHED
*/
private void populateWidget() {
Intent widgetUpdateIntent = new Intent();
widgetUpdateIntent.setAction(WidgetProvider.DATA_FETCHED);
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
sendBroadcast(widgetUpdateIntent);
this.stopSelf();
}
}
JSONfunctions.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
Hope this helps someone and Great thanks to ( https://stackoverflow.com/users/739306/laaptu ) for the tutorials.
I'm developing an app and now I have to pass a parameter to a RESTful Service's URL. I'm using AsyncTask, and I need to pass a text from a list view as a parameter to the URL, for example: the URL is http://ip:7001/product?product_name=PARAM I need to get the text from the selected item from my list view, and pass as a parameter in PARAM, using AsyncTask. I've already got the text from the item in the listView, now I just need to pass it as a parameter.
This is my AsycTask class:
package com.tumta.henrique.teste;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.tumta.henrique.teste.ProdutoFragment;
/**
* Created by Henrique on 18/05/2015.
*/
public class FiltraProduto extends AsyncTask<String, Void, List<String>> {
private ConsultaConcluidaFiltroProdutoListener listener;
public static String URL_STRING = "http://192.168.0.20:7001/com.henrique.rest/api/v1/status/pro_filtro?pro_nome=";
public FiltraProduto(ConsultaConcluidaFiltroProdutoListener listener) {
this.listener = listener;
}
private List<String> InterpretaResultado(String resultado) throws JSONException {
JSONObject object = new JSONObject(resultado);
JSONArray jsonArray = object.getJSONArray("produto");
//JSONObject jsonProduto = jsonArray.getJSONObject(0);
// String id = jsonProduto.getString("pro_id");
//proId = id;
List<Object> listaNomes = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonProdutoInfo = jsonArray.getJSONObject(i);
String proNome= jsonProdutoInfo.getString("pro_nome");
double proPreco = jsonProdutoInfo.getDouble("pro_preco");
double proSdAtual = jsonProdutoInfo.getDouble("pro_sdAtual");
listaNomes.add(i, proNome);
listaNomes.add(i, proPreco);
listaNomes.add(i, proSdAtual);
}
List<String> strings = new ArrayList<String>();
for (Object o : listaNomes) {
strings.add(o != null ? o.toString() : null);
}
return strings;
}
private String ConsultaServidor() throws IOException {
InputStream is = null;
try {
URL url = new URL(URL_STRING);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
conn.getResponseCode();
is = conn.getInputStream();
Reader reader = null;
reader = new InputStreamReader(is);
char[] buffer = new char[2048];
reader.read(buffer);
return new String(buffer);
} finally {
if (is != null) {
is.close();
}
}
}
#Override
protected List<String> doInBackground(String... params) {
try {
String resultado = ConsultaServidor();
return InterpretaResultado(resultado);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(List<String> result) {
listener.onConsultaConcluida(result);
super.onPostExecute(result);
}
public interface ConsultaConcluidaFiltroProdutoListener {
void onConsultaConcluida(List<String> result);
}
}
In the URL_STRING I need to pass the param at pro_nome=?
Here I get the item text. This is in my Fragment that has the List View:
public String retornaParam(String param){
return param;
}
#Override
public void onConsultaConcluida(List<String> result) {
final ListView listaProdutos = (ListView) getView().findViewById(R.id.listaprodutos);
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getView().getContext(),android.R.layout.simple_list_item_1, result);
listaProdutos.setAdapter(arrayAdapter);
listaProdutos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
String nomeProduto = listaProdutos.getItemAtPosition(position).toString();
retornaParam(nomeProduto);
Intent intent = new Intent(getActivity(), DetalhesProdutoActivity.class);
//intent.putExtra("pro_nome", listaProdutos.getItemAtPosition(position).toString());
startActivity(intent);
}
});
}
I get the text and store it in param from the retornaParam method.
Does somebody know how to do it?
If you need more information, just let me know.
You pass in params to an AsyncTask using:
YourAsyncTask.execute(yourview.getText(), "and", "more", "params");
You can then access them in
#Override
protected String doInBackground(String... params) {
URL_STRING += params[0];
...
Just add the following code before sending executing your httpClient:
URL_STRING + = textInsideYourTextView;
It should work, just avoid to manipulate your ui elements outside your UI thread.
this is my code for getting data from a PHP site into a ListView.
package be.pressd.arrangementen;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button fetch;
EditText et;
String aantalPersonen;
private ListView lv;
private ArrayAdapter<String> listAdapter ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fetch = (Button) findViewById(R.id.fetchButton);
et = (EditText) findViewById(R.id.aantalPersonen);
// Find the ListView resource.
lv = (ListView) findViewById(R.id.arrangementenLijst);
listAdapter = new ArrayAdapter<String>(this, R.layout.line_row);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), "Click nummer " + position, Toast.LENGTH_LONG).show();
String arrangement = (String) lv.getItemAtPosition(position);
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ArrangementItem.class);
// sending data to new activity
i.putExtra("arrangement", arrangement);
startActivity(i);
}
});
fetch.setOnClickListener(this);
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Fetching data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
#Override
protected Void doInBackground(String... params) {
String url_select = "http://mywebsite/thephpform.php?aantpers=" + aantalPersonen;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try
{
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
try
{ listAdapter.clear();
JSONArray Jarray = new JSONArray(result);
for(int i=0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = null;
Jasonobject = Jarray.getJSONObject(i);
String name = Jasonobject.getString("naam");
listAdapter.add(name);
}
this.progressDialog.dismiss();
lv.setAdapter( listAdapter);
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.fetchButton :
aantalPersonen = et.getText().toString();
if (aantalPersonen.trim().equals("")) {
Toast.makeText(this, "Gelieve het aantal personen in te geven", Toast.LENGTH_SHORT).show();
return;
}
else
{
new task().execute();
break;
}
}
}
}
It is my first Android code ever, so besides my question it is possible that this code can be made better.
What I would like to do is to show ALL data, nicely, which was gotten from the website. But, as a ListView can not contain the ID and other data, I'm wondering if I can reuse the data in the JSONObject to be shown in next screen (on click of ListView item) ?
Greetings and thanks in advance,
Davy
Create variables for which you want to store information and loop through JsonArray and get each JsonObject and parse/extract information that you need and store it in variables.
here is sample code to iterate JsonArray //Here response is JsonArray
Create Simple class like
public class PersonInfo{
String name,address; //Create variables of your requirement.
//Add Getter Setter Methods for these variables
}
//End of class PersonInfo
ArrayList<PersonInfo> persons = new ArrayList<PersonInfo>();
PersonInfo person;
JSONObject product;
try
{
for (int j = 0; j < response.length(); j++)
{
person = new Person();
product = response.getJSONObject(j);
person.name = product.getString("JSON_KEY_NAME"); //like these assign values to each variable of PersonInfo class
//Other values initialization
}
persons.add(person); // Add PersonInfo object to ArrayList
}
catch (Exception e)
{
e.printStackTrace();
}
something like that .
you can get json values upon your requirments.
This is just sample code .
You could
save the JSON and parse it again later (you would have to do a lot of parsing)
save the data you parse from the json
in sharedpreferences (only if small amount of data)
in a local sqlite db (probably cleanest approach with best user experience, but a lot of work since you have to create a class that does all the db CRUD operations)
It depends on your data. For user preferences I would use SharedPreferences, for larger amounts a local DB. Also you could create some objects to store the data you need, maybe as signletons or something, and parse the stored JSONs everytime you start the app.
See the google docs on data storage for reference.
To save data you are getting in background task you have to use shared preferences.
Refer this link to get details about shared preferences.
To pass data to next page you have to use following code :
Intent intent = new Intent( currentActivity.this, NextActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
On nextActivity.java add following code inside onCreate()
Intent currentPageIntent = getIntent();
String strValue = currentPageIntent.getStringExtra("key");
If you are having any other data type than string you have to use other method instead of getStringExtra()
To get id of item clicked, you can use following code :
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
JSONArray jsonArray = new JSONArray(jsonData);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
if (i == id) {
//Add your code here
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally { }
}
});
Note that you have to process jsonData on your own. This is just sample code I used in one of my porjects.
I'm new to android and I hope someone could help me here. I have an activity Faculty and a button.
This is my XML layout browse_faculty:
<Button
android:onClick="searchFSTEHandler"
android:id="#+id/bFSTE"
android:layout_width="220dp"
android:layout_height="80dp"
android:layout_alignLeft="#+id/bFBE"
android:layout_below="#+id/bFBE"
android:layout_marginTop="20dp"
android:text="#string/fste" />
and this is my Faculty Activity which displays the buttons:
I use Intent to view ListView
public class Faculty extends Activity{
#Override
protected void onCreate(Bundle BrowseFaculty) {
// TODO Auto-generated method stub
super.onCreate(BrowseFaculty);
setContentView(R.layout.browse_faculty);
}
//on the XML,this is the "searchFSTEHandler" i want to use to show ListView
public void searchFSTEHandler(View target){
Intent courseList = new Intent(this, HttpActivity.class);
startActivity(courseList);
}
}
and below is the "HttpActivity" class is the class that displays my ListView. This class read a php file which gets data from a MySQL server and converts to JSON data then parses it into a array list.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
public class HttpActivity extends ListActivity {
public String f1 = "FSTE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faculty_course);
new getHttp().execute();
getHttp test =new getHttp();
String strJsonData = test.doInBackground(f1);
// Convert String JSON data to Java class
ArrayList<Courses> arrayCourse= test.parseJsonData(strJsonData);
//Create an ArrayAdapter which shows the ArrayList data
this.setListAdapter(new ArrayAdapter<Courses>(this,android.R.layout.simple_list_item_1,arrayCourse));
}
private class getHttp extends AsyncTask<String, Void, String> {
public getHttp() {
}
#Override
protected String doInBackground(String... faculty) {
InputStream is = null;
try {
URL url = new URL("http://10.0.2.2/sqlWebService.php?faculty=FSTE");
URLConnection con = url.openConnection();
con.setConnectTimeout(10000);
con.setReadTimeout(10000);
is = con.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String str;
while ((str = br.readLine()) != null) {
sb.append(str);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
} finally {
try {
if (is != null)
is.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
//------------------------------------------------------
private ArrayList<Courses> parseJsonData(String strJson) {
ArrayList<Courses> Course = new ArrayList<Courses>();
try {
// Generate JSONArray object by JSON String data
JSONArray arr = new JSONArray(strJson);
//from the JSONArray, get one element (row) of JSONData
for (int i = 0; i < arr.length(); i++) {
//Get JSON Object which is one element of the array
JSONObject ob = arr.getJSONObject(i);
Courses exam = new Courses();
//get the value by key, and set to exam class
exam.setCode(ob.optString("code"));
//save exam class to exam array list
Course.add(exam);
}
} catch (JSONException e) {
e.printStackTrace();
}
return Course;
}
}
}
The application crashes as soon as I click on the button and gives a error:
"android.os.NetworkOnMainThreadException"
Help Please !
The problem that you are having is network operation like what you are trying to do cannot and should not be performed on the main UI thread. Doing so will lead to an ANR (Android Not Responding), which is exactly the kind of error you are getting just now. What you want to do is move your code to a separate thread or to an AsyncTask that will perform this action in the background on a different thread using the doInBackground() method which does not have access to your views on the main UI thread. For example,
private class ExampleOperation extends AsyncTask<String, Void, String> {
public ExampleOperation() { //ctor }
#Override
protected void onPreExecute() {
//things that you want to initialize and maybe show dialog to user.
}
#Override
protected String doInBackground(String... params) {
//this is where you perform that network related operation.
}
#Override
protected void onPostExecute(String result) {
//this is where get the results from the network related task.
}
#Override
protected void onProgressUpdate(Void... values) {
//you can update your progressbar if any; otherwise omit method.
}
}
Then all you have to do is call the AsyncTask where ever you want to use it: new ExampleOperation().execute();
You can't make HTTP calls in the main thread, they take too long and would make the UI unresponsive. You need to do it in an AsyncTask.