Skip to content

Instantly share code, notes, and snippets.

@tristaaan
Created October 29, 2014 23:20
Show Gist options
  • Save tristaaan/41f0ff90745a612916ac to your computer and use it in GitHub Desktop.
Save tristaaan/41f0ff90745a612916ac to your computer and use it in GitHub Desktop.
GET and POST in Android
//import stuff
public void sendPOST(final String url, final ArrayList<NameValuePair> params){
Thread t = new Thread(new Runnable(){
@Override
public void run(){
try {
HttpClient client = new DefaultHttpClient();
String postURL = url;
HttpPost post = new HttpPost(postURL);
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("POST RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
public void sendGET(final String url){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
String getURL = url;
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
Log.i("GET RESPONSE", EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment