Advertisement
scm22ri

APIConnectorClass

Sep 24th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. package com.example.mywebsite.com;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.StringWriter;
  11. import java.net.URLEncoder;
  12.  
  13. import org.apache.commons.io.IOUtils;
  14. import org.apache.http.HttpEntity;
  15. import org.apache.http.HttpResponse;                                                                                                               
  16. import org.apache.http.client.ClientProtocolException;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.util.EntityUtils;
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import android.annotation.SuppressLint;
  25. import android.util.Log;
  26.  
  27. public class APIConnector {
  28.  
  29.     public JSONArray GetAllCustomers(){
  30.        
  31.         String url = "http://mywebsite.com/getState.php";
  32.    
  33.         HttpEntity httpEntity = null;
  34.    
  35.         try{
  36.              
  37.              DefaultHttpClient httpClient = new DefaultHttpClient();
  38.              HttpGet httpGet = new HttpGet(url);
  39.                
  40.              HttpResponse httpResponse = httpClient.execute(httpGet);
  41.                
  42.              httpEntity = httpResponse.getEntity();
  43.              
  44.         } catch(ClientProtocolException e){
  45.             e.printStackTrace();
  46.            
  47.         } catch(IOException e){
  48.             e.printStackTrace();
  49.         }
  50.        
  51.        
  52.         JSONArray jsonArray = null;
  53.        
  54.         if(httpEntity !=null){
  55.             try{
  56.                 String entityResponse = EntityUtils.toString(httpEntity);
  57.                
  58.                 Log.e("Entity Response : ", entityResponse);
  59.                
  60.                 jsonArray = new JSONArray(entityResponse);
  61.             } catch(JSONException e){
  62.                 e.printStackTrace();
  63.             } catch(IOException e){
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.    
  68.         return jsonArray;
  69.     }
  70.    
  71.    
  72.     // @SuppressLint("NewApi")
  73.     public JSONArray GetCityDetails(String StateID) {
  74.  
  75.         @SuppressWarnings("deprecation")
  76.         String url = "http://mywebsite.com/getCity.php?StateID="+URLEncoder.encode(StateID);
  77.  
  78.         HttpEntity httpEntity = null;
  79.    
  80.         try{
  81.              
  82.              DefaultHttpClient httpClient = new DefaultHttpClient();
  83.              HttpGet httpGet = new HttpGet(url);
  84.                
  85.              HttpResponse httpResponse = httpClient.execute(httpGet);
  86.                
  87.              httpEntity = httpResponse.getEntity();
  88.              
  89.            
  90.         } catch(ClientProtocolException e){
  91.             e.printStackTrace();
  92.            
  93.         } catch(IOException e){
  94.             e.printStackTrace();
  95.         }
  96.        
  97.         // JSONObject jsonObject = null;
  98.         // JSONArray jsonArray = null;
  99.         JSONArray jsonArray = new JSONArray();
  100.         if(httpEntity !=null){
  101.             try{
  102.                 // make another function that reads an input stream, accepts parameters and returns string value
  103.                 // httpEntity = (HttpEntity) httpEntity.getContent();              
  104.                
  105.                 // String entityResponse = EntityUtils.toString((HttpEntity) httpEntity.getContent());
  106.                 // String entityResponse = EntityUtils.toString((HttpEntity) httpEntity.getContent());
  107.                
  108.                 InputStream entityResponse = httpEntity.getContent();
  109.                 // String entityResponseAfterFunctionCall = convertInputStreamIntoString(entityResponse);
  110.                 // String entityResponseAfterFunctionCall2 = readFully(entityResponse);
  111.                 String entityResponseAfterFunctionCall3 = letsDoThisAgain(entityResponse);
  112.                 // String theString = IOUtils.toString(entityResponse);
  113.                 Log.e("Entity Response From GetCityDetails Class: ", entityResponseAfterFunctionCall3);
  114.                
  115.                 jsonArray = new JSONArray(entityResponseAfterFunctionCall3);
  116.                 // jsonObject=new JSONObject(entityResponse);
  117.             } catch(JSONException e){
  118.                 e.printStackTrace();
  119.             } catch(IOException e){
  120.                 e.printStackTrace();
  121.             }
  122.         }
  123.  
  124.         return jsonArray;
  125.         // return jsonObject;
  126.     }
  127.    
  128.     // http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
  129.     // http://stackoverflow.com/questions/2492076/android-reading-from-an-input-stream-efficiently/3552721#3552721
  130.     // http://stackoverflow.com/questions/3091359/install-apache-common-lang-2-5-in-eclipse
  131.     // http://stackoverflow.com/questions/179024/adding-a-jar-to-an-eclipse-java-library
  132.     // http://commons.apache.org/proper/commons-io/download_io.cgi
  133.     public String convertInputStreamIntoString(InputStream entityResponse) throws IOException{
  134.        
  135.         // this method needs the apache commons to work. I downloaded the commons but getting this error
  136.         // java.lang.noclassdeffounderror org.apache.commons.io.ioutils
  137.         // I guess the path directory is messed up?
  138.         StringWriter writer = new StringWriter();
  139.         IOUtils.copy(entityResponse, writer);
  140.         String theString = writer.toString();
  141.         return theString;
  142.     }
  143.  
  144.     public String readFully(InputStream entityResponse) throws IOException {
  145.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  146.         byte[] buffer = new byte[1024];
  147.         int length = 0;
  148.         while ((length = entityResponse.read(buffer)) != -1) {
  149.             baos.write(buffer, 0, length);
  150.         }
  151.         return baos.toString("UTF-8");
  152.     }
  153.    
  154.     public String letsDoThisAgain(InputStream entityResponse){
  155.        
  156.         BufferedInputStream bis = new BufferedInputStream(entityResponse);
  157.         InputStreamReader is = new InputStreamReader(entityResponse);
  158.         StringBuilder sb = new StringBuilder();
  159.         BufferedReader br = new BufferedReader(is);
  160.        
  161.         try {
  162.             String read = br.readLine();
  163.            
  164.             while(read !=null){
  165.                 sb.append(read);
  166.                 read = br.readLine();
  167.             }
  168.    
  169.        
  170.         } catch (IOException e) {
  171.             e.printStackTrace();
  172.         }
  173.        
  174.         return sb.toString();  
  175.  
  176.     }
  177.    
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement