0

I am trying to implement an app that upon opening, starts to upload the contacts to localhost database (apache), while showing a progress indicator. I want to do this as an asynchronous task.

this is my code so far, and i am feeling abit lost on how to proceed from here.

Main Activity:

public class MainActivity extends Activity{

private ProgressBar pb;

   private ProgressDialog progress;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      progress = new ProgressDialog(this);
   }


   public void open(View view){
      progress.setMessage("Downloading Contacts ");
      progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progress.setIndeterminate(true);
      progress.show();


class MyAsyncTask extends AsyncTask<String, Integer, Double>{

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub
        postData(params[0]);
        return null;
    }

    protected void onPostExecute(Double result){
        pb.setVisibility(View.GONE);
    }
    protected void onProgressUpdate(Integer... progress){
        pb.setProgress(progress[0]);
    }

    public void postData(String valueIWantToSend) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(localhost/reg.php");

        try {
            // WHAT DO I ADD HERE?? A LITTLE CONFUSED ABOUT THIS PART TOO
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    }
   }

}

Omar Younis
  • 57
  • 1
  • 7
  • Check out this [answer](http://stackoverflow.com/a/7059083/336557) (along with its comments). I think it may help you. – Mahm00d Jul 12 '14 at 19:47

1 Answers1

0

Procedure what you are doing is wrong. PostExecute is used to interact with user interface to display result. DoInBackground where you will perform high operation like parsing etc.,

PreExecute is used to display some progress dialogue to parse and display the result.

Please check this tutorial. Hope you will get it.

Connect android through async task

Shadow
  • 6,864
  • 6
  • 44
  • 93