Reputation: 47
I'm using HttpGet method for retrieving data from a web service from my Android app. Can anyone tell me how to convert the below given code to HttpPost method?
String url = URLEditor.encode("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection?TechCode="+username+"&TechPIN="+password);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity == null) return false;
is = entity.getContent();
Thanks in advance...
thanks for helping me..
I tried with the code given above. But I get Document object as NULL. This's the code
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection");
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
nvpList.add(new BasicNameValuePair("TechCode", techcode));
nvpList.add(new BasicNameValuePair("TechPIN", techpin));
httpPost.setEntity(new UrlEncodedFormEntity(nvpList));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
I get doc as NULL. No issues when I use HttpGet. How can it be solved? Please help
Upvotes: 0
Views: 1371
Reputation: 3366
Here is the code, this will help you.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("TechCode",username));
nameValuePairs.add(new BasicNameValuePair("TechPIN",password));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
Upvotes: 2