Java Android : Request Using method Post return respond as String


Here is the code :

public String postData(String rUrl) {
     // Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(rUrl);
InputStream content = null;

String s = “”;
String line = “”;

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);

nameValuePairs.add(new BasicNameValuePair(“nama”, “Kokom”));
nameValuePairs.add(new BasicNameValuePair(“alamat”, “bandung”));
nameValuePairs.add(new BasicNameValuePair(“email”, “ycccc@xxxx.com”));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

content = response.getEntity().getContent();

BufferedReader rd = new BufferedReader(new InputStreamReader(content));
try {
while ((line = rd.readLine()) != null) { s += line; }
} catch (IOException e) {
Log.d(“[Stream to String]”, “Stream to String Error. “, e);
return null;
}
        return s;

} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
return null;
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s