/* call this function to post a data on webservice*/
static public String postHTPPRequest(String URL, String paramenter)
{
Log.i("aa",""+paramenter);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setHeader("Content-Type", "application/json");
Log.i("aadd",""+httppost);
try
{
if(paramenter != null)
{
StringEntity tmp = null;
tmp = new StringEntity(paramenter,"UTF-8");
httppost.setEntity(tmp);
}
HttpResponse httpResponse = null;
httpResponse = httpclient.execute(httppost);
int code = httpResponse.getStatusLine().getStatusCode();
System.out.print("hello==========="+code);
Log.i("hh", ""+code);
/* if(code == 200)
{*/
HttpEntity entity = httpResponse.getEntity();
if(entity != null)
{
byte[] Data = new byte[256];
//int len = 0;
InputStream input = null;
input = entity.getContent();
String res=convertStreamToString(input);
input.close();
System.out.println("****************postHTPPRequest::response = " + new String(Data));
System.out.println("post=="+new String(Data));
return res;
}
//}
}
catch(Exception e)
{
Log.e("","ERROR"+ e);
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
Log.e("","ERROR"+e);
} finally {
try {
is.close();
} catch (IOException e) {
Log.e("","ERROR"+e);
}
}
return sb.toString();
}
}
static public String postHTPPRequest(String URL, String paramenter)
{
Log.i("aa",""+paramenter);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setHeader("Content-Type", "application/json");
Log.i("aadd",""+httppost);
try
{
if(paramenter != null)
{
StringEntity tmp = null;
tmp = new StringEntity(paramenter,"UTF-8");
httppost.setEntity(tmp);
}
HttpResponse httpResponse = null;
httpResponse = httpclient.execute(httppost);
int code = httpResponse.getStatusLine().getStatusCode();
System.out.print("hello==========="+code);
Log.i("hh", ""+code);
/* if(code == 200)
{*/
HttpEntity entity = httpResponse.getEntity();
if(entity != null)
{
byte[] Data = new byte[256];
//int len = 0;
InputStream input = null;
input = entity.getContent();
String res=convertStreamToString(input);
input.close();
System.out.println("****************postHTPPRequest::response = " + new String(Data));
System.out.println("post=="+new String(Data));
return res;
}
//}
}
catch(Exception e)
{
Log.e("","ERROR"+ e);
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
Log.e("","ERROR"+e);
} finally {
try {
is.close();
} catch (IOException e) {
Log.e("","ERROR"+e);
}
}
return sb.toString();
}
}
No comments:
Post a Comment