Reputation: 5619
I need a little hand holding transitioning from C# to Java / Android for a client to consume a web service async of course so it doesn't lock the gui....(a progress bar would be icing on the cake.) In C# I would create the object do something like this in the client.
Create a service reference. Create an object that matches the service interface members and populate. Launch service request passing object created in step two in a separate thread. wait showing progress bar. Handle response whether error or return data.
Now perhaps I'm being too simplistic but I assumed in Java it would be pretty close to this method. What I am seeing is alot of people saying to extend the AsyncTask class. What I am missing is how I pass my values from the client? For example how do I send the username and password? I'm afraid all the different articles and nuances are confusing me. I just need a straight forward way to get the job done.
So I would be grateful for any tips, pointer, tutorials or other such reading.
JB
Upvotes: 1
Views: 3334
Reputation: 3268
6 months back, I started Android development with background in C#/.NET
First option which we are currently using is AsyncTask to perform a task async... which in this case is consuming web services...
A sample code is
public class AsyncHttpRequestManager extends AsyncTask<String, Boolean, String> {
private final String serviceUrl = "URL HERE";
@Override
protected String doInBackground(String... params) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpPut putRequest = new HttpPut(serviceUrl);
putRequest.setHeader("Content-type", "text/plain");
putRequest.setHeader("Accept", "text/plain");
putRequest.setEntity(new StringEntity(params[0]));
HttpResponse response = client.execute(putRequest);
InputStream content = response.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String result = "";
String s = "";
while ((s = buffer.readLine()) != null) {
result += s;
}
return result;
}
catch (UnsupportedEncodingException e) {
Log.e("AsyncOperationFailed", e.getMessage());
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.e("AsyncOperationFailed", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("AsyncOperationFailed", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
Log.d("MESSAGE", result);
}
}
I have written a blog post on this.
Further for displaying progress/loading dialog you may have to override onPreExecute and onPostExecute methods in AsyncTask. Check the documentation.
The second alternate is to use loopj library which is an asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries.
An example code utilizing loopj is
AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth("username","password/token");
client.get("http://www.example.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
}
});
Over the time, we have created our helpers over the HttpClient and therefore I would suggest you to go for a open source library such as loopj which would really help you climb the initial steps quickly. The only challenge is that when you come from another platform you may face initial challenge with other libraries rather than simple AsyncTask but it's worth spending time, if you have flexibility.
Upvotes: 0
Reputation: 755
public class AsyncHttpRequestManager extends AsyncTask<String, Boolean, String> {
private final String serviceUrl = "URL HERE";
String user;
String pass;
public AsyncHttpRequestManager(String user, String Pass){
this.user = user;
this.pass = pass;
}
@Override
protected String doInBackground(String... params) {
//execute the post
return "";
}
protected void onPostExecute(String result) {
Log.d("MESSAGE", result);
//update the UI with the results
}
}
and you execute the call using
new AsyncHttpRequestManager(user, pass).execute();
Upvotes: 1
Reputation: 6862
Async tasks bring with them a lot of issues due to the fact that the activity they are hosted on might be destroyed while the execution is still ongoing.
The best approach is to host the request inside an intentservice or inside a service, which will survive among the requesting activity.
If you want a working out of the box solution I'd suggest giving a look at robospice library, which is a quite mature project. It's based on this Google Io 2010 talk
Upvotes: 1