Created
October 29, 2014 23:20
-
-
Save tristaaan/41f0ff90745a612916ac to your computer and use it in GitHub Desktop.
GET and POST in Android
This file contains 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
//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