I am developing an application with android, I am new to it in one part of my projects I need to get the data from input fields from users, then creates a Http connection to server and put the result of the first call along with some other fields into another Http connection to Server, I don't know what is the right approach to implement it.
public class TwoHttpActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_transport);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public static class PlaceholderFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
params[0] = firstText;
. . . .
. . . .
params[3] = secondText;
}
});
}
}
The Question: Now I need params[0] to be sent to one url http://myserver/urlOne and then get the result from the server and put them along with params[1] to params[3] and send them together to the second url of server http://myserver/urlTwo
Since these connections should be made inside classes
extends from AsyncTask
then I think it's not possible to just have
public class FirstUrlTask extends AsyncTask<String[], Void, Void> {
//the class handles the call to first url inside
//doInBackground(String[]... params)
}
and
public class SecondUrlTask extends AsyncTask<String[], Void, Void> {
//the class handles the call to second url inside
//doInBackground(String[]... params)
}
and then
firstUrlTask.execute(..);
secondUrlTask.execute(..);
inside button.setOnClickListener(new View.OnClickListener()
Since they are asynchronous task it's not clear which tasks will be executed first, so please help me with the right approach to do this, please provide your answer with detailed information and code sample.
Do it like this:
First Step: Call FirstUrlTask Async Class
Second Step: Call SecondUrlTaskAsync Class in onPostExecute() of FirstUrlTask
The above approach will ensure that the first call will be made to first url and on the response of first url, the second url will be called.