Created
April 12, 2014 19:16
-
-
Save meadhikari/10551834 to your computer and use it in GitHub Desktop.
Post request with android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.httprequest; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class MainActivity extends Activity { | |
EditText et; | |
Button btn; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
et = (EditText) findViewById(R.id.editText1); | |
btn = (Button) findViewById(R.id.button1); | |
new LongOperation().execute(""); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
public void process(View v) { | |
// Toast.makeText(getApplicationContext(), et.getText().toString(), | |
// Toast.LENGTH_SHORT).show(); | |
} | |
private class LongOperation extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
HttpClient httpclient = new DefaultHttpClient(); | |
HttpPost httppost = new HttpPost("http://localhost/gcg/insert.php"); | |
try { | |
// Add your data | |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); | |
nameValuePairs.add(new BasicNameValuePair("id", "12345")); | |
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); | |
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 | |
} | |
return "Executed"; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
// TextView txt = (TextView) findViewById(R.id.output); | |
// txt.setText("Executed"); // txt.setText(result); | |
// Toast.makeText(getApplicationContext(), | |
// et.getText().toString(),Toast.LENGTH_SHORT).show(); | |
// might want to change "executed" for the returned string passed | |
// into onPostExecute() but that is upto you | |
} | |
@Override | |
protected void onPreExecute() { | |
} | |
@Override | |
protected void onProgressUpdate(Void... values) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment