2

I am new to android development. I am trying out a basic app. My requirement is that I need to send data from a text box in an android app to a PHP page that is hosted on net using POST method. I have browsed many sites for tutorial on this method, but didn't find a right one. Can anyone please provide me an example code for sending data from ONE textbox in an android app to a PHP page using post method? Thank you in advance.

1 Answer 1

4

Something like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText input = (EditText)findViewById(R.id.yourinput);
    input.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String text = input.getText().toString();
            new UploadTask().execute(text);
        }
    });
}

private class UploadTask extends AsyncTask<String, Integer, String> {

    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Uploading...");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://yourwebsite.com/commit.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs
                    .add(new BasicNameValuePair("username", params[0]));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            if (response != null) {
                InputStream in = response.getEntity().getContent();
                String responseContent = inputStreamToString(in);

                return responseContent;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        // process the result
        super.onPostExecute(result);
    }

    private String inputStreamToString(InputStream is) throws IOException {
        String line = "";
        StringBuilder total = new StringBuilder();

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        // Read response until the end
        while ((line = rd.readLine()) != null) { 
            total.append(line); 
        }

        // Return full string
        return total.toString();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Just a quick comment about the inputStreamToString. You can actually avoid this with: HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF8");
Just saw the full comment. Cannot test it atm but might edit it into my answer then :) thank you
Jup sorry, I tried to linebreak and it sent my comment too early.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.