Combined GET and POST requests HttpURLConnection - android

I am looking for some advise about the following, I have a HttpUtil class (util) in which I have my sendGetRequest- and sendPostRequest methods.
I use them to perform a successfull login to a website from which I am going to fetch some data.
Now, I call these methods in my MainActivity in a Asynctask:
protected Boolean doInBackground(Void... params) {
try {
//1. GET request
util.sendGetRequest(loginURL, null);
//2. Post request
util.sendPostRequest(loginURL, null);
//3. Final GET request
util.sendGetRequest(resultURL, null);
// Read stream
String[] response = util.readMultipleLinesRespone();
for (String line : response) {
System.out.println(line);
}
} catch (InterruptedException e) {
return false;
} catch (Exception e) {
e.printStackTrace();
}
}
I am looking for a solution so that one waits for another to finish (1st get then post, finally get), if it`s possible I want to keep this util.class intact and not put everything in the Asynctask (doInBackground).
Is this possible or do I have the wrong approach?
Your opinions and advise please.
Thank you in advance.

The approach I normally use is this:
First, consider using an Event for instance a library like this that will not only decouple your code but also make it so easy to notify your activity when the first HttpGet request is completed.
Next, start your GET request inside doInBackground and then notify your activity once the process is completed - normally inside onPostExecute
When in your activity, assuming you use the EventBus library, you can execute the next AsyncTask which will do your next task like doing a POST request accordingly.
Using this approach should really make your life easier - specifically helps you know when something is completed and so you can proceed with the next task as needed.
Good luck!

You can have two AsyncTasks. One for get and other for post. First execute get request. Then in onPostExecute of the first AsyncTask execute the second AsyncTask that sends the post request in doInBackground();

Your HttpUtil class should somehow signal that the response is received.
For example, put this code in HttpUtil to signal end of operation:
synchronized (this) {
done = true;
notifyAll();
}
and this code will wait for the operation to end:
while(!util.isDone()) {
try {
synchronized(util) { util.wait(); }
} catch(Exception e) { ... }
}

Related

Make One AsyncTask Wait For Another One To Complete?

I have two Asynchronous tasks each one in a separate class, I can call them in the main thread, simply using:
new RetrieveTask().execute();
new RetrieveTaskImageData().execute();
But I want the first one to finish before starting the second.
This is an example of one of them:
class RetrieveTask extends AsyncTask<String, Void,Void> {
private Exception exception;
protected Void doInBackground(String... urls) {
try {
//Code here
} catch (Exception e) {
this.exception = e;
} finally {
}
return null;
}
protected void onPostExecute() {
//
}
}
How can we achieve this?
EDIT
Can we use new RetrieveTask().execute().getStatus()==..Finished ?
Thank you
This will be achieved automatically because unless you call executeOnExecutor, all AsyncTasks run on the same thread, so two of them can not run concurrently.
I'have 4 async tasks in my main thread and have if statement inside 2 of them, if one works, other one stops. I did removecallback and execute just inside tasks for eachother, worked for me. (i am using kotlin but i think same should work with java)
You have to impelement interface on first AsyncTask and onPostExecute it will callback to main thread. you need to call second AsyncTask on that override callback method.

Network connection on main thread - alternative in this case?

I need to get a color in the server and set it as a theme
setTheme(colorId);
it needs to be set inside onCreate() and at the beginning to work.
I'm doing this:
protected void onCreate(Bundle savedInstanceState) {
getColor();
...
}
public void getColor() {
StrictMode.ThreadPolicy tp = StrictMode.ThreadPolicy.LAX; //force network on main thread
StrictMode.setThreadPolicy(tp);
OkHttpClient client = new OkHttpClient();
okhttp3.Request request = new okhttp3.Request.Builder()
.url("http://myip/color.php")
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
String color = response.body().string();
setTheme(color);
} catch (IOException e) {
e.printStackTrace();
}
}
}
So, I'm forcing a network connection on main thread because if I add doInBackground(), on response... it will run after my onCreate() and the color will not be set.
My question here is:
1) Is it the only way?
2) Can I do things without force network on main thread?
3) I'd like an example, if possible, not just: "yes, it is possible" or "do XYZ" because I'm new to Android and without examples it doesn't help much.
You shouldn't make any network requests on the main thread.
Instead, you should show the user a progress bar whilst it fetches the data on a background thread.
BUT: Since you have to set the theme in the onCreate() it means you can't show the user any progress because the views won't be inflated at that point. A better solution would be to get the color from the server on a previous activity and pass it in as and intent parameter.
It's always a bad idea to force the network to the main thread
Replace the UI blocking call
client.newCall(request).execute();
With an asynchronous call
client.newCall(request).enqueue(new Callback() {
Check the documentation on how that all works
if I add doInBackground, on response... it will run after my onCreate and the color will not be set
In an Asynctask? Then you're doing something wrong. Okhttp doesn't need an Asynctask

Frequent usage of asynctask

I have created an AsyncTsak class in my project which downloads some information from web server. I am sure that it works well because when it is called by onCreate() , I can see the result . But unfortunately when I call it again via a button it doesn't work.
I am not sure but i think i have read somewhere about this problem . It said , we are permitted to use AsyncTask class only once.
AsyncTask class
class DownloadAdvertismentLevel2 extends AsyncTask<String,String,String>{
String [] ImageInformation=null;
protected void onPreExecute(){
// do nothing !
}
protected String doInBackground(String...Urls){
String Data="";
HttpURLConnection urlConnection = null;
try{
URL myUrl=new URL("http://10.0.2.2:80/Urgence/ads.aspx?Action=DownloadIds&TB="+TopBoundry+"&LB="+LowBoundry);
urlConnection = (HttpURLConnection)myUrl.openConnection();
BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));
String temp="";
// Data is used to store Server's Response
while((temp=in.readLine())!=null)
{
Data=Data+temp;
}
}
catch(Exception ex){
Log.d("Er>doInBackground", ex.toString());
urlConnection.disconnect();
}
finally{
urlConnection.disconnect();
}
return Data;// it sends Result to onPostExcute
}
protected void onPostExecute(String Data){
createPhotoAlbum();
pb.closedProg();
}
}
onCreate
Here I don't have any problem . It works fine
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ads);
new DownloadAdvertismentLevel2().execute();
}
Via Button
ButtonSeeMore.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Counting();
}});
Counting
public void Counting(){
if(TotalRows-6>0){
TopBoundry=TotalRows;
LowBoundry=TotalRows-6;
TotalRows=LowBoundry;
}
new DownloadAdvertismentLevel2().execute();
}
Please consider that I need to use this class till it shows the information. What would you suggest ?
To expand on what JVN said about AsyncTask
Each instance of Async task can only be executed once.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
http://developer.android.com/reference/android/os/AsyncTask.html (Under 'Threading Rules')
This doesn't stop you making a new instance -
public void Counting(){
if(TotalRows-6>0){
TopBoundry=TotalRows;
LowBoundry=TotalRows-6;
TotalRows=LowBoundry;
}
new DownloadAdvertismentLevel2().execute();
new DownloadAdvertismentLevel2().execute();
}
^ The code above will run your task twice.
Your code looks fine.
I would guess that the problem is (in order of likelihood)
1) On click isn't working
2) Post Execute isn't working as expected
3) The server response isn't being read correctly
4) Your Server isn't handling the request properly
But this would be obvious if you run your debugger or add some extra log outputs
1) I think you might be able to use the Async task only once in the class. But definitely it can be called multiple times.
2) please check if your button onclicklistener() function is really getting called on button click. try some logs in that.
because the code seems to be fine.
To allow multiple asycnh tasks run at the same time you need to use the 'executeOnExceuter mechanism:
See this note from the Android doucmentation:
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.
An example invocation would look like (this particular example is from a video manipulation app - the 'distributionTask' is an instance of a class that extends AsynchTask):
//Now execute synch task - to allow multiple AsynchTasks execute in parallel the
//'executeOnExecutor' call is required. It needs to be used with caution to avoid the usual synchronization issues and also
//to avoid too many threads being created
distributionTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Environment.getExternalStorageDirectory() + "/videoChunk_"+i+".mp4");

What may prevent the UI in android to be updated on time?

I'm developing an Android app that has to update it's UI depending on receiving and processing some server responses, I'm using runOnUiThread for that. I have like five activities in that app, all is working very well but one requires me to relaunch the Activity(like going to another one and then returning to it) or interacting with it in order to that update takes place, and that is the way i'm using with all the Activities including the infected one:
runOnUiThread(new Runnable() {
public void run() {
try {
response_received(response);
} catch (Exception e) {
e.printStackTrace(); // never catch any Exceptions
}
}
});
private static void response_received(JSONObject response) throws Exception{
try {
int volume_setted = response.getInt(volume);
Normal_Activity.volume_value.setText(String.valueOf(volume_setted)); // the Volume TextView updated efficiently
Infected_Activity.volume_value.setText(String.valueOf(volume_setted)); // has the problem mentioned above
} catch (JSONException ex) {
}
}
I'm pretty sure the problem is not in the TextView as all the Activity UI has this problem but i just posted an example.
Do not directly set values in a Activity from another Activity. If you want to pass data to Another activity always use Intents. check the below link
pass data from intent to an other intent
If you want to start another activity and get result back check the below link
http://developer.android.com/training/basics/intents/result.html

Android UI Thread freezes waiting for socket in a simple client/server architecture

I think this is a quite common problem, but still I didn't find a satisfactory answer so I'm going to ask myself.
This is a piece of code:
// this is insine OnClickView
TextView status = (TextView) findViewById(R.id.status);
status.setText("Trying to connect to the server...");
try {
// this opens a socket and send a login request to the server.
int result = CommunicationManager.login(String email, String password);
switch (result) {
case CommunicationManager.SUCCESS:
// login ok, go on with next screen
break;
case CommunicationManager.WRONG_EMAIL:
status.setTextColor(Color.RED);
status.setText("Wrong Email!");
break;
case CommunicationManager.WRONG_PASSWORD:
status.setTextColor(Color.RED);
status.setText("Wrong Password!");
break;
}
} catch (CommunicationException e) {
status.setTextColor(Color.RED);
status.setText("Unable to estabilish a connection!");
} catch (ProtocolException e) {
status.setTextColor(Color.RED);
status.setText("Protocol error!");
}
This is what I would like to achieve:
User click Send button;
status textview shows "Trying to connect to the server...";
UI "waits" for communications to be over;
status textview shows result accordingly.
But instead when user clicks Send button, UI freezes (oddly before status text appears) until communication is done (I tried to connect to an unknown host).
A quick fix is to set a socket timeout, but I don't like this kind of solution: UI still freezes and which timeout should be set?
My first thought were Thread obviously, but as you can see I need to return a value, thing that in threading environment doesn't make much sense since threads run independently and asynchronously.
So what I need is definitely that UI waits for the service to be executed but without freezing.
By the way it seems to me that waiting for a return value means that UI has to wait for the task to be over, I just would not let it freeze.
I came across AsyncTask but I see two major disadvantages:
it seems to me that is too tightly coupled with UI;
what if I want to execute service with Integer, String and Boolean parameters? Should I extend AsyncTask<Object, Void, Void>?
Both leads to inextensibility.
What can I do to achieve my goal?
Please note that another request to the service will be a request for something that could not be ready yet, so I should automatically repeat request every few time (let's say ten minutes). So probably I'll need something I can use with TimerTask, but I'll still need to return a value to UI every time I execute that service (so I can update the status text and let the user know what's going on).
This is typical use case while dealing through external communication i.e. HTTP calls.
Best way is to use AsyncTask. To give you answers for your concerns for AsyncTask.
it seems to me that is too tightly coupled with UI;
Here good code design will play a role. You can write you own call back mechanism to get rid of tight coupling. Example can be below.
Create your version for request and response you need for WS call. It can be very simple primitive type or complex type parameters.
class Result{
//Define more para.
}
class Request{
//Deinf more para.
}
Write below callback interface.
public interface MyCallBack {
public void onComplete(Result result);}
Create AsyncTask and get above Interface object in constructor, same object can return Result object.
class LongRunningTask extends AsyncTask<Request, Integer, Long>{
private MyCallBack callback;
public LongRunningTask(MyCallBack callback) {
super();
this.callback = callback;
}
#Override
protected Long doInBackground(Request... params) {
// Perform your back ground task.
return null;
}
#Override
protected void onPostExecute(Long result) {
super.onPostExecute(result);
callback.onComplete(new Result()); //Here result is dummy but in real it should be contructred from doInBackground() method
}
}
Now last and important part to implement the interface and call asynctask. I am trying to reuse your code to have better clarity.
public class MainActivity extends Activity implements MyCallBack{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView status = (TextView) findViewById(R.id.status);
status.setText("Trying to connect to the server...");
}
private void onClick(){
//Similer to CommunicationManager.login(String email, String password); in your code.
LongRunningTask longRunningTask = new LongRunningTask(this);
longRunningTask.execute(new Request());
}
#Override
public void onComplete(Result result) {
try {
int result = result.getStatus
switch (result) {
case CommunicationManager.SUCCESS:
// login ok, go on with next screen
break;
case CommunicationManager.WRONG_EMAIL:
status.setTextColor(Color.RED);
status.setText("Wrong Email!");
break;
case CommunicationManager.WRONG_PASSWORD:
status.setTextColor(Color.RED);
status.setText("Wrong Password!");
break;
}
} catch (CommunicationException e) {
status.setTextColor(Color.RED);
status.setText("Unable to estabilish a connection!");
} catch (ProtocolException e) {
status.setTextColor(Color.RED);
status.setText("Protocol error!");
}
}
what if I want to execute service with Integer, String and Boolean parameters? Should I extend AsyncTask?
First Parameter is any user defined para. In case you need to pass multiple parameters then put them in to form of entity (i.e. - Class). Also, you can pass initial configuration parameter in constructor of AsyncTask i.e. - Communication URL.
Hope it will help.
Use multi threading, do all the communication in a different thread
Use worker thread, or AsyncTask for doing long-running operations.
Moreover, from Android Honeycomb, system throws exception, if you perform network operations on UI thread.

Categories

Resources