开发者

How to convert HttpGet to HttpPost?

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", techpi开发者_JAVA百科n));
    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


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());
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜