Advertisement
scm22ri

APIConnectorPrices

Sep 23rd, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. public JSONArray GetCityDetails(String StateID) {
  2.  
  3.     @SuppressWarnings("deprecation")
  4.     String url = "http://mywebsite.com/getCity.php?StateID="+URLEncoder.encode(StateID);
  5.  
  6.     HttpEntity httpEntity = null;
  7.  
  8.     try{
  9.  
  10.          DefaultHttpClient httpClient = new DefaultHttpClient();
  11.          HttpGet httpGet = new HttpGet(url);
  12.  
  13.          HttpResponse httpResponse = httpClient.execute(httpGet);
  14.  
  15.          httpEntity = httpResponse.getEntity();
  16.  
  17.  
  18.     } catch(ClientProtocolException e){
  19.         e.printStackTrace();
  20.  
  21.     } catch(IOException e){
  22.         e.printStackTrace();
  23.     }
  24.  
  25.  
  26.     JSONArray jsonArray = null;
  27.     if(httpEntity !=null){
  28.         try{
  29.  
  30.             InputStream entityResponse = httpEntity.getContent();
  31.             // not working
  32.             String entityResponseAfterFunctionCall2 = readFully(entityResponse);
  33.             // not working
  34.             String entityResponseAfterFunctionCall3 = letsDoThisAgain(entityResponse);
  35.             Log.e("Entity Response: ", entityResponseAfterFunctionCall3);
  36.  
  37.             jsonArray = new JSONArray(entityResponseAfterFunctionCall3);
  38.  
  39.         } catch(JSONException e){
  40.             e.printStackTrace();
  41.         } catch(IOException e){
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.  
  46.     return jsonArray;
  47. }
  48.  
  49. public String readFully(InputStream entityResponse) throws IOException {
  50.     ByteArrayOutputStream baos = new ByteArrayOutputStream();
  51.     byte[] buffer = new byte[1024];
  52.     int length = 0;
  53.     while ((length = entityResponse.read(buffer)) != -1) {
  54.         baos.write(buffer, 0, length);
  55.     }
  56.     return baos.toString();
  57. }
  58.  
  59. public String letsDoThisAgain(InputStream entityResponse){
  60.  
  61.     InputStreamReader is = new InputStreamReader(entityResponse);
  62.     StringBuilder sb = new StringBuilder();
  63.     BufferedReader br = new BufferedReader(is);
  64.     try {
  65.         String read = br.readLine();
  66.  
  67.         while(read !=null){
  68.             sb.append(read);
  69.             read = br.readLine();
  70.         }
  71.  
  72.     } catch (IOException e) {
  73.         e.printStackTrace();
  74.     }
  75.  
  76.     return sb.toString();  
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement