I am creating an android application which has to execute web requests in the background and then handle the received data and modify the user interface according to the server response.
The goal of posting requests and handling data in the background is to avoid the freezing of user interface. Currently however I notice that the user interface is freezing so I am not sure the logic is working as it is supposed to.
Here is the part of code which is supposed to post requests and handle responses in its own thread and then pass the data to GUI:
public class ServerConnection {
Queue<String> requests;
...
DefaultHttpClient httpClient;
HttpHost targetHost;
Handler handler;
ServerResponseHandler responseHandler;
Activity activity;
public ServerConnection(Activity activity){
this.activity = activity;
this.responseHandler = (ServerResponseHandler) activity;
httpClient = new DefaultHttpClient();
targetHost = new HttpHost(TARGET_DOMAIN, 80, "http");
requests = new LinkedList<String>();
}
private Runnable requestSender = new Runnable(){
#Override
public void run() {
if(!requests.isEmpty()){
String requestString = requests.remove();
HttpGet httpGet = new HttpGet(requestString);
httpGet.addHeader("Accept", "text/xml");
String encodingString = "testuser:testpass";
String sEncodedString = Base64Coder.encodeString(encodingString);
try{
String sContent = fetchURL(requestString, sEncodedString);
XMLParser xmlParser = new XMLParser();
List <Product> products = xmlParser.getProducts(sContent);
responseHandler.onProductsResponse(products);
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}
}
};
public void sendRequest(String requestString){
requests.add(requestString);
handler = new Handler();
handler.post(requestSender);
}
The method sendRequest() is called from the main activity which implements ServerResponseHandler. I guess the request is executed in its own thread and by calling
responseHandler.onProductsResponse(products);
the list of products (data from the web) is passed to main activity. Anyway due to poor performance I would appreciate if anyone could correct any possible issue in the logic above or suggest any other (better) option.
I'd suggest you take a look at ASyncTask class (available since Android 1.5).
It simplifies the process of creating a background Thread that synchronizes with the GUI thread once it's complete.
You should be able to achieve what you're trying using code something list this
private class DownloadFilesTask extends AsyncTask<String, List<Product>, Integer> {
protected List<Products> doInBackground(String... requestStrings) {
int count = requestStrings.length;
int results = 0;
for (int i = 0; i < count; i++) {
String requestString = requestStrings[i];
HttpGet httpGet = new HttpGet(requestString);
httpGet.addHeader("Accept", "text/xml");
String encodingString = "testuser:testpass";
String sEncodedString = Base64Coder.encodeString(encodingString);
try{
String sContent = fetchURL(requestString, sEncodedString);
XMLParser xmlParser = new XMLParser();
List <Product> products = xmlParser.getProducts(sContent);
results++;
publishProgress(products);
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}
return results;
}
protected void onProgressUpdate(Integer... progress) {
// TODO You are on the GUI thread, and the first element in
// the progress parameter contains the last progress
// published from doInBackground, so update your GUI
}
protected void onPostExecute(int result) {
// Processing is complete, result contains the number of
// results you processed
}
}
And execute by calling
new DownloadFilesTask().execute(url1, url2, url3);
According to the handler javadoc, I don't think the post() method create any threads. If I'm right it execute the Runnable on the thread to which the handler is attached. So in this case this is the activity thread so the UI thread ! That's why you have poor performance.
You have to implement a Thread which execute your Runnable. But by doing that, you won't be able to update your activity like you currently do by calling :
responseHandler.onProductsResponse(products);
This is because you are not any more in the UI thread, and only the UI thread is authorized to interact with the UI (so the activity).
So you have to replace this call by accessing your Handler.
Message msg = handler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putSerializable( "products", products ); //not sure it will pass here
msg.setData( bundle );
handler.sendMessage( msg );
And implementing the handleMessage() method for your Handler :
#Override
public void handleMessage( Message msg )
{
List <Product> products = msg.getData().getSerializable( "products" );
responseHandler.onProductsResponse(products);
}
Last but not least : the Handler has to still be created in the activity thread.
Related
I want to ask if this that looks like an issue to me is problem.
I have a class of AsyncTask to get data from a json file and a doInBackground method with pre-execute and post-execute methods.
At onCreate method of my MainActivity I call the class of AsyncTask with name.execute(). The problem is that the program stuck into the post execute method, is that a problem? There is a way to return to the OnCreate method or should I continue with my code from post execute method?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new LoadAllProducts().execute();
txtView=(TextView) findViewById(R.id.txtV);
}
class LoadAllProducts extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading questions. 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 questions: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
questions = json.getJSONArray(TAG_QUESTIONS);
// looping through All Products
for (int i = 0; i < questions.length(); i++) {
JSONObject c = questions.getJSONObject(i);
// Storing each json item in variable
int id = c.getInt(TAG_QID);
String questionPron = c.getString(TAG_QUESTION);
String answer1 = c.getString(TAG_ANSWER_1);
String answer2 = c.getString(TAG_ANSWER_2);
String answer3 = c.getString(TAG_ANSWER_3);
String answer4 = c.getString(TAG_ANSWER_4);
int level = c.getInt(TAG_LEVEL);
int correctIs = c.getInt(TAG_CORRECT_IS);
// String updatedAt = c.getString(TAG_UPDATED_AT);
dokimi = questionPron;
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
//ArrayList<eachQuestion> qArray = new ArrayList<eachQuestion>();
eachQuestion ea = new eachQuestion();
ea.setId(id);
ea.setQuestionPron(questionPron);
ea.setAnswer1(answer1);
ea.setAnswer2(answer2);
ea.setAnswer3(answer3);
ea.setAnswer4(answer4);
ea.setLevel(level);
ea.setCorrectIs(correctIs);
// adding each child node to HashMap key => value
//map.put(TAG_PID, id);
//map.put(TAG_NAME, name);
qArray.add(ea);
}
}
} 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() {
}
});
}
}
"The problem is that the program stuck into the post execute method, is that a problem?" I can only guess what that was supposed to mean but I will try to answer your question. The reason why AsyncTask even exists is that its code is run on a separate thread (cpu). The main thread (cpu) is making another cpu execute given code.
That is why method call of execute() returns almost immediately, most probably even before any line of given code for another cpu executes. You are not able to predict when exactly (if ever) this code will execute. This depends on your OS's scheduler and current runtime session, in computer science we describe such behaviour as undeterministic.
i m new android and find the best approach to update the data on UI with thread-safety for getting data from server.
here is my code to get data from server api and update list view,
but i was think this approach may cause to degrade the performance of application.
i m using okhttp for calling server api.
public String ActionAsyncGetTeamData(final String URL, final String token,final String expected_closure_date, final HTTPVerb verb, Activity activity) {
final String[] ResponseData = new String[1];
loading_complete=false;
_context = activity;
if (networkDetector.isNetworkAvailable()) {
new Thread(new Runnable() {
public void run() {
ResponseData[0] = GET(URL, token, expected_closure_date);
leadList.clear();
meetingList.clear();
taskList.clear();
TeamSyncModel teamSyncModel=json.fromJson(ResponseData[0],TeamSyncModel.class);
for (LeadModel leadModel: teamSyncModel.lead) {
leadList.add(leadModel);
}
for (MeetingModel meetingModel: teamSyncModel.meeting) {
meetingList.add(meetingModel);
}
for (TaskModel taskModel: teamSyncModel.task) {
taskList.add(taskModel);
}
loading_complete=true;
_context.runOnUiThread(new Runnable() {
#Override
public void run() {
Planner planner=new Planner();
planner.progress.setVisibility(View.GONE);
planner.list.setVisibility(View.VISIBLE);
planner.adapterTeam(_context);
planner.closure_header.requestFocus();
}
});
}
}).start();
return ResponseData[0];
}
else {
Planner planner=new Planner();
planner.progress.setVisibility(View.GONE);
planner.list.setVisibility(View.VISIBLE);
planner.adapterTeam(_context);
leadList.clear();
meetingList.clear();
taskList.clear();
loading_complete=true;
Toast.makeText(_context.getApplicationContext(), "Internet connection not available", Toast.LENGTH_SHORT).show();
}
return null;
}
Get data from Server
private String GET(String url, String token, String lastSync) {
client.setConnectTimeout(15, TimeUnit.MINUTES);
client.setReadTimeout(15, TimeUnit.MINUTES);
try {
Request request = new Request.Builder()
.url(url + lastSync)
.addHeader("x-access-token", token)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
} catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
is it right way to do this tasks in android
you try the following steps
1 in UI create refer to listview, adapter and asyntask
2 from the ui send reference to adapter at the asyntask in this getData from server and in the method onPostExecute of the asynk call this method adapter.notifyDatasetChange();
3 in the adapter you manage the info what you give of the server in the asyntask
i've done a rest web service that gives me some contact information like numbers, age ... i get all this information in this function
public static void getRest(String search) {
if(search.equals("")){
json="http://localhost:8080/com.vogella.jersey.first/rest/jsonServices/print/";
} else {
json="http://localhost:8080/com.vogella.jersey.first/rest/jsonServices/print/"+search;
}
ConnectionRequest req = new ConnectionRequest() {
#Override
protected void postResponse() {
}
#Override
protected void readResponse(InputStream input) throws IOException {
JSONParser p = new JSONParser();
Map<String, Object> h = p.parseJSON(new InputStreamReader(input));
ArrayList object=new ArrayList();
for (Entry<String, Object> entry : h.entrySet()) {
object = (ArrayList) entry.getValue();
int i=object.size();
}
for(int i=0; i<object.size();i++){
LinkedHashMap s= (LinkedHashMap) object.get(i);
Risultati.add(s);
}
}
};
req.setUrl(json);
req.setPost(false);
req.addRequestHeader("Accept", "application/json");
InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
req.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueue(req);
Risultati is an attribute of the class: ArrayList<LinkedHashMap> Risultati;
the problem is that when i call the function getRest("") in this way:
getRest("");
Label contatto=null;
for(int j=0;j<Risultati.size();j++){
LinkedHashMap s=Risultati.get(j);
String nome=(String) s.get("firstName");
String cognome=(String) s.get("lastName");
String numero =(String) s.get("numero");
contatto=new Label(nome+" "+cognome+" "+numero);
}
hi.addComponent(contatto);
it turns that Risultati is null, if i comment the for cycle i notice that the inner function readResponse is executed after...i don't know what i'm doing wrong
I think the point is that you're calling NetworkManager.getInstance().addToQueue(req). According to it's documentation, it will add a connection request (the one you've just created) to a queue. After the connection request is added to the queue, it returns, meaning the request may or may not have been executed by that time.
You have to options to deal with this. In my opinion, the best way would be to update the user interface after the request has completed, as described in the "File System, Storage, Network & Parsing" chapter of the CodeOne manual:
req.addResponseListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
NetworkEvent e = (NetworkEvent)ev;
// ... process the response
}
});
Alternatively, you could replace the call to addToQueue(req) with addToQueueAndWait(req). The latter method waits until the request is processed in the queue. The downside of the latter approach is that your user interface may freeze while the request is being processed, because the UI thread is blocked on the network I/O.
I have a custom adapter that buttons and TextView, the TextView is changed on a button click within listview and it after sending and receiving feedback from http Post via Json response, I tried using runnable and assynctask but no success. In the runnable, I can not return a value from the method.
What I wanted is to send http request to the server and return json results, based on the returned results, the TextView will change.
What is the best approach to use to achieve this.
If any one can help point me to a resource that will help me to achieve this will be highly welcome.
Thanks in advance!
Here is my code..
public String getStatus(final String id, final String user){
final String[] resp = {"0/0/0"};
new Thread(new Runnable() {
#Override
public void run() {
// Building Parameters
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", user));
params.add(new BasicNameValuePair("id", id));
Log.d("Nay Params", "" + params);
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(NAY_URL, "POST",
params);
// check your log for json response
// json success tag
try {
Log.d("Nay Response", ""+ json);
success = json.getBoolean(TAG_SUCCESS);
yeaSt = json.getBoolean(TAG_E_STATUS);
naySt = json.getBoolean(TAG_A_STATUS);
yeaC = json.getInt(TAG_YEA);
nayC = json.getInt(TAG_NAY);
if (success){
resp[0] = yeaS + "/" + naySt + "/" + yeaC + "/" + nayC;
return resp[0];
//Can not return return value within void method that is; Class void run()
}
} catch (JSONException e) {
e.printStackTrace();
//return resp[0];
}
}
}).start();
//Can not acces json result outside the try catch block
Log.d("sux", ""+success);
// Log.d("Rest1", resp[0]);
return resp[0];
}
You could use Callable interface in order to run a thread and get the thread's result.
Here's the Callable documentation.
And Jakob explains how to use it here.
Hope that helped, if you need a concrete example I recently used this approach in my project, and I'll be happy to provide you with a sample.
Edit:
Here is a simple example of using callable, I changed a little bit my code so it is more clear:
public class Test {
ExecutorService executor;
public Test() {
/* I used an executor that creates a new thread every time creating a server requests object
* you might need to create a thread pool instead, depending on your application
* */
executor = Executors.newSingleThreadExecutor();
}
private JSONObject doTheWork() {
// init
Callable<JSONObject> callable;
Future<JSONObject> future;
JSONObject jsonResult = null;
try {
// create callable object with desired job
callable = new Callable<JSONObject>() {
#Override
public JSONObject call() throws Exception {
JSONObject jsonResult = new JSONObject();
// connect to the server
// insert desired data into json object
// and return the json object
return jsonResult;
}
};
future = executor.submit(callable);
jsonResult = future.get();
} catch(InterruptedException ex) {
// Log exception at first so you could know if something went wrong and needs to be fixed
} catch(ExecutionException ex) {
// Log exception at first so you could know if something went wrong and needs to be fixed
}
return jsonResult;
}
}
Another approach would be creating a class that implements Callable. Whatever of these approaches you choose depends on your application, and maybe person's taste :).
Happy that I helped.
So I'm trying to make a youtube application by retrieving the json data from a youtube channel. Problem is I'm great at writing single-threaded applications. But when it comes to multithreading I always lose my insight.
I haven't learned it in school yet and most tutorials are rubbish, or atleast I find that.
So what I want to do is introduce a thread to my "GetVideos" class so it doesn't slow down my application while retrieving the videos. I know I'll have to use a handler and thread but everytime I try to use them my application crashes. Can you guys help?
public class GetVideos { //class that retrieves JSON data based on youtube username
private String channelName;
private HttpClient client; //client that gets info
private VideoLibrary lib;
public GetVideos(String channelName) {
this.channelName = channelName;
client = new DefaultHttpClient();
lib = new VideoLibrary();
fillData();
}
private void fillData() {
try {
final String URL = "http://gdata.youtube.com/feeds/api/users/" + channelName + "/uploads?v=2&alt=jsonc";
HttpGet get = new HttpGet(URL);
HttpResponse response = client.execute(get);
String jsonString = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject items = jsonArray.getJSONObject(i);
String title = items.getString("title");
String thumbUrl = items.getJSONObject("thumbnail").getString("sqDefault");
String url = items.getJSONObject("player").getString("default");
lib.addVideo(new Video(title, url, thumbUrl));
}
} catch (JSONException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClientProtocolException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace();
}
}
public VideoLibrary getLib() {
return lib;
}
}
public class SxePhil extends Activity { //class that makes up the interactive end of
//the app, here the JSON data is put in a list
private ListView list;
private GetVideos g;
private VideoLibrary videoLibrary;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sxephil);
list = (ListView) findViewById(R.id.sxephilList);
g = new GetVideos("sxephil");
videoLibrary = g.getLib();
ArrayList<String> titles = new ArrayList<String>();
for (int i = 0; i < 25; i++) {
titles.add(videoLibrary.getVideos().get(i).getTitle());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow, titles);
list.setAdapter(adapter);
}
}
Basically what I tried was implementing the threading that is used here: http://blog.blundell-apps.com/show-youtube-user-videos-in-a-listview/
in my code, because this project was built on an older sdk, so I wanted to modernize it so to speak. Look at the GetYoutube*** class and MainActivity class that's where the money is
You should either use an AsyncTask or even better, a Loader, they automatically handle the treads for you
First : basic advices (that you MUST remeber and follow)
Never do processing in a class constructor (that's bad OO design, imho)
Never, ever leave time-consuming process in Activity.onCreate method (and in general, in any method that is runned by the UI Thread, ie. onResume(), onClick(), ...)
mario and runor49 were right : you should use an AsyncTask here (and you must read and understand http://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads. If you don't, read it again...)
Basically, you should do the following :
onCreate must launch a separate thread, which will contain the "long" processing (load the data from network)
When done, this thread must call the Activity.runOnUiThread(Runnable), where the runnable will create the Adapter and assign it to the ListView.
Here is a code sample :
#Override
public void onCreate(Bundle savedInstanceState) {
...
loadDataInThread();
}
private void loadDataInThread() {
new Runnable() {
#Override
public void run() {
// LOAD DATA FROM NETWORK
displayDataInUIThread(datas);
}
}.start();
}
private void displayDataInUIThread(final VideoLibrary data) {
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow, data);
list.setAdapter(adapter);
}
});
}
This is a very basic (and rude) way to do it, and the AsyncTask way () way will be far better, but it should be working and fill the contract...
You can accomplish this most easily using an AsyncTask. Check out this tutorial.
http://android-developers.blogspot.com/2009/05/painless-threading.html