Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>6 months back, I started Android development with background in C#/.NET</p> <p><strong>First option</strong> which we are currently using is <a href="http://developer.android.com/reference/android/os/AsyncTask.html" rel="nofollow">AsyncTask</a> to perform a task async... which in this case is consuming web services...</p> <p>A sample code is </p> <pre><code>public class AsyncHttpRequestManager extends AsyncTask&lt;String, Boolean, String&gt; { 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); } } </code></pre> <p>I have written a <a href="http://www.adilmughal.com/2012/07/android-networkonmainthread-exception.html" rel="nofollow">blog post</a> on this.</p> <p>Further for displaying progress/loading dialog you may have to override onPreExecute and onPostExecute methods in AsyncTask. Check the documentation.</p> <p>The <strong>second alternate</strong> is to use <a href="http://loopj.com/android-async-http/" rel="nofollow">loopj library</a> which is an asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. </p> <p>An example code utilizing loopj is </p> <pre><code>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); } }); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload