Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you are trying to do some operations with a web service in Android, you have to use AsyncTask to prevent the network on main thread error, here's a sample code I used when uploading something:</p> <pre><code>public class PostDataAsyncTask extends AsyncTask&lt;String, String, String&gt; { protected void onPreExecute() { super.onPreExecute(); // do stuff before posting data } @Override protected String doInBackground(String... strings) { try { // 1 = post text data, 2 = post file int actionChoice = 2; // post a text data if(actionChoice==1){ postText(); } // post a file else{ postFile(); } } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String lenghtOfFile) { // do stuff after posting data } // this will post our text data private void postText(){ try{ // url where the data will be posted String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php"; Log.v(TAG, "postURL: " + postReceiverUrl); // HttpClient HttpClient httpClient = new DefaultHttpClient(); // post header HttpPost httpPost = new HttpPost(postReceiverUrl); // add your data List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2); nameValuePairs.add(new BasicNameValuePair("firstname", "Mike")); nameValuePairs.add(new BasicNameValuePair("lastname", "Dalisay")); nameValuePairs.add(new BasicNameValuePair("email", "mike@testmail.com")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // execute HTTP post request HttpResponse response = httpClient.execute(httpPost); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { String responseStr = EntityUtils.toString(resEntity).trim(); Log.v(TAG, "Response: " + responseStr); // you can add an if statement here and do other actions based on the response } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>How the AsyncTask was used:</p> <pre><code>new PostDataAsyncTask().execute(); </code></pre> <p><a href="http://www.codeofaninja.com/2013/04/android-http-client.html" rel="nofollow">Here's a reference for you.</a></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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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