Thursday, 25 August 2011

Httppost method in android

    private  static HttpClient httpClient;
    private  static String ret;
    private  static HttpPost httpPost;
    private  static HttpResponse response;
    private  static HttpContext localContext;
public static InputStream postStage(String url, String data,String soapAction) {
        // TODO Auto-generated method stub
        InputStream is = null;
        ret = null;
        httpClient=new DefaultHttpClient();
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
        httpPost = new HttpPost(url);
        response = null;
        StringEntity tmp = null;    
        if(Constants.LOG)Log.d("url is"+url,"poststage"+data);
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
            "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
       
        httpPost.setHeader("Accept", "text/html,application/xml," +
            "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
       
        httpPost.setHeader("Content-Type", " text/xml; charset=utf-8");
//       httpPost.setHeader("Content-Length",""+data.getBytes().length);
        httpPost.setHeader("SOAPAction", soapAction);
           try {
               tmp = new StringEntity(data,"UTF-8");
           } catch (UnsupportedEncodingException e) {
               if(Constants.LOG)Log.d("HTTPHelp : UnsupportedEncodingException : ",""+e);
           }
           httpPost.setEntity(tmp);
          
           HttpParams httpParameters = new BasicHttpParams();
           HttpConnectionParams.setConnectionTimeout(httpParameters, Constants.CONNECTION_TIMEOUT);
           HttpConnectionParams.setSoTimeout(httpParameters, Constants.CONNECTION_TIMEOUT);
           try {
               DefaultHttpClient httpClient = new  DefaultHttpClient(httpParameters);
               response =(BasicHttpResponse) httpClient.execute(httpPost);
           }catch(UnknownHostException e){
               if(Constants.LOG)Log.d("HTTPHelp : ClientProtocolException : ",""+e);
               e.printStackTrace();
           }catch(ConnectTimeoutException e){
               if(Constants.LOG)Log.d("HTTPHelp : ClientProtocolException : ",""+e);
               e.printStackTrace();
           }catch (ClientProtocolException e) {
               if(Constants.LOG)Log.d("HTTPHelp : ClientProtocolException : ",""+e);
               e.printStackTrace();
           }catch (IOException e) {
               if(Constants.LOG)Log.d("HTTPHelp : IOException : ",""+e);
               e.printStackTrace();
           }catch (Exception e) {
                // TODO: handle exception
               e.printStackTrace();
               return null;
           }
           try {
               ret = response.getStatusLine().toString();
               HttpEntity he = response.getEntity();
               InputStream inStream = he.getContent();
               is = inStream;
               Log.d("input stream is",""+inStream);
           } catch (IllegalStateException e) {
             // TODO Auto-generated catch block
               e.printStackTrace();
           } catch (IOException e) {
             // TODO Auto-generated catch block
               e.printStackTrace();
           }
           catch (Exception e) {
               // TODO: handle exception
               return null;
           }
           if(Constants.LOG)Log.d("in", "post"+is);
   
           return is;
    }//postStage()

No comments:

Post a Comment