7 Mar 2012

WebApiRequest.

Today i am posting some methods which is used while you parsing data from server.   Enjoy :)
import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.os.Bundle;
import android.util.Log;

public class WebAPIRequest
{     
       // Return's Document
       public Document performGet(String url)
       {
              Document doc = null;
              try {
                     DefaultHttpClient client = new DefaultHttpClient();
                     URI uri = new URI(url);
                     HttpGet method = new HttpGet(uri);
                     HttpResponse res = client.execute(method);
                     InputStream data = res.getEntity().getContent();
                     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                     DocumentBuilder db = dbf.newDocumentBuilder();
                     doc = db.parse(data);
              } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                     // e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (URISyntaxException e) {
                     // TODO Auto-generated catch block
                     //e.printStackTrace();
              } catch (ParserConfigurationException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (SAXException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }

              return doc;
       }

       // Return's Document
       public Document performPost(String apiUrl, Bundle bundle)
       {
              // TODO Auto-generated method stub
              Document doc = null;
              try {
                     HttpClient client = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost(apiUrl);
                     HttpResponse res;
                     httppost.setEntity(new UrlEncodedFormEntity(getListOfNameValuePair(bundle))); 
                     res = client.execute(httppost);
                     InputStream data = res.getEntity().getContent();

                     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                     DocumentBuilder db = dbf.newDocumentBuilder();
                     doc = db.parse(data);

              } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (ParserConfigurationException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (SAXException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              return doc;
       }

       // Return's String
       public String performGet_String(String url) {
              String doc = null;
              try {
                     DefaultHttpClient client = new DefaultHttpClient();
                     URI uri = new URI(url);
                     HttpGet method = new HttpGet(uri);
                     HttpResponse res = client.execute(method);
                     InputStream data = res.getEntity().getContent();

                     StringBuilder stringBuilder = new StringBuilder();
                     int b;
                     while ((b = data.read()) != -1)
                     {
                           stringBuilder.append((char) b);
                     }

                     doc = stringBuilder.toString();
              } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                     // e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (URISyntaxException e) {
                     // TODO Auto-generated catch block
                     //e.printStackTrace();
              }

              return doc;
       }

       // Return's String
       public String performPost_String(String apiUrl, Bundle bundle)
       {
              // TODO Auto-generated method stub
              String doc = null;
              try {
                     HttpClient client = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost(apiUrl);
                     HttpResponse res;
                     httppost.setEntity(new UrlEncodedFormEntity(getListOfNameValuePair(bundle))); 
                     res = client.execute(httppost);
                     InputStream data = res.getEntity().getContent();

                     StringBuilder stringBuilder = new StringBuilder();
                     int b;
                     while ((b = data.read()) != -1)
                     {
                           stringBuilder.append((char) b);
                     }

                     doc = stringBuilder.toString();

              } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              return doc;
       }



       public Document performSOAPPost(String url, String body, String soapAction){
              Document doc= null;
              Log.i("Upload XMl body", body);
              try {
                     HttpPost httppost = new HttpPost(url);         
                     StringEntity se = new StringEntity(body,HTTP.UTF_8);
                     se.setContentType("text/xml");
                     httppost.setHeader("Content-Type","text/xml; charset=utf-8");
                     httppost.setHeader("SOAPAction", soapAction);
                     httppost.setHeader("Host", "api.my-t.co.il:9008");
                     //httppost.setHeader("Content-Length", String.valueOf(body.length()));
                     //httppost.setHeader("Content-Length", "1067");
                     httppost.setHeader("Expect", "100-continue");
                     httppost.setHeader("Connection", "Keep-Alive");
                     httppost.setEntity(se);
                     HttpClient httpclient = new DefaultHttpClient();
                     BasicHttpResponse res = (BasicHttpResponse) httpclient.execute(httppost);
                     InputStream data = res.getEntity().getContent();

                     //System.out.println(res.getStatusLine());
                     //String data1 = convertStreamToString(data);
                     //Log.i("Response :::", data1);
                     //Log.i("XML  ::::: ", data1);
                     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                     DocumentBuilder db = dbf.newDocumentBuilder();
                     doc = db.parse(data);
              }catch (ClientProtocolException e)
              {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }


              return doc;
       }

       //Make List Of NameValuePair
       public static  List<NameValuePair> getListOfNameValuePair(Bundle parameters)
       {
              List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
              if (parameters == null) return null;
              for (String key : parameters.keySet())
              {
                     nameValuePairs.add(new BasicNameValuePair(key, parameters.getString(key)));
              }
              return nameValuePairs;
       }
       public static String convertStreamToString(InputStream is) throws IOException {
              if (is != null) {
                     StringBuilder sb = new StringBuilder();
                     String line;
                     try {
                           BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                           while ((line = reader.readLine()) != null) {
                                  sb.append(line).append("\n");
                           }
                     }
                     catch (Exception e) {
                           // TODO: handle exception
                           e.printStackTrace();
                     }
                     finally {
                           is.close();
                     }
                     return sb.toString();
              } else {       
                     return "false";
              }
       }


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)


No comments:

Post a Comment