


wadael wrote:HI Moons,
Thanks a lot for your code, you saved me time as I'm new to Android.
I did some refactoring so that it can be used and reused in one's projects.Using java Syntax Highlighting
package org.wadael.android.utils.net; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; 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; /** * A refactoring of the code provided by Moons on this page * doing_http_post_with_the_current_sdk-t5911.html * * Allows to send POST requests to a configurable server * * @author Moons, Wadael * */ public class HTTPPoster { public static HttpResponse doPost(String url, Map<String, String> kvPairs) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); if (kvPairs != null && kvPairs.isEmpty() == false) { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( kvPairs.size()); String k, v; Iterator<String> itKeys = kvPairs.keySet().iterator(); while (itKeys.hasNext()) { k = itKeys.next(); v = kvPairs.get(k); nameValuePairs.add(new BasicNameValuePair(k, v)); } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } HttpResponse response; response = httpclient.execute(httppost); return response; } } Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Usage : in the onClick of a button.
SERVER is a URI constant (="http://.......")Using java Syntax Highlighting
public void onClick(View v) { String url = SERVER.toASCIIString(); Map<String, String> kvPairs = new HashMap<String, String>(); kvPairs.put("key1","value1"); kvPairs.put("key2","value2"); try { HttpResponse re = HTTPPoster.doPost(url, kvPairs); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); // Do something } } Parsed in 0.032 seconds, using GeSHi 1.0.8.4








<?php>
echo "My id is ". $_POST['id']


public String POST(String action, String var1, String var2) {
String answer = null;
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Your DATA
nameValuePairs.add(new BasicNameValuePair("action", action));
nameValuePairs.add(new BasicNameValuePair("var1", var1));
nameValuePairs.add(new BasicNameValuePair("var2", var2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
String ReTurned = EntityUtils.toString(resEntity);
answer = ReTurned;
Toast.makeText(this, answer, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(this, "There was an issue Try again later", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this, "There was an IO issue Try again later", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return answer;
} HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
Users browsing this forum: No registered users and 3 guests