Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mywebsite.com;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.StringWriter;
- import java.net.URLEncoder;
- import org.apache.commons.io.IOUtils;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.annotation.SuppressLint;
- import android.util.Log;
- public class APIConnector {
- public JSONArray GetAllCustomers(){
- String url = "http://mywebsite.com/getState.php";
- HttpEntity httpEntity = null;
- try{
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url);
- HttpResponse httpResponse = httpClient.execute(httpGet);
- httpEntity = httpResponse.getEntity();
- } catch(ClientProtocolException e){
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- JSONArray jsonArray = null;
- if(httpEntity !=null){
- try{
- String entityResponse = EntityUtils.toString(httpEntity);
- Log.e("Entity Response : ", entityResponse);
- jsonArray = new JSONArray(entityResponse);
- } catch(JSONException e){
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- return jsonArray;
- }
- // @SuppressLint("NewApi")
- public JSONArray GetCityDetails(String StateID) {
- @SuppressWarnings("deprecation")
- String url = "http://mywebsite.com/getCity.php?StateID="+URLEncoder.encode(StateID);
- HttpEntity httpEntity = null;
- try{
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url);
- HttpResponse httpResponse = httpClient.execute(httpGet);
- httpEntity = httpResponse.getEntity();
- } catch(ClientProtocolException e){
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- // JSONObject jsonObject = null;
- // JSONArray jsonArray = null;
- JSONArray jsonArray = new JSONArray();
- if(httpEntity !=null){
- try{
- // make another function that reads an input stream, accepts parameters and returns string value
- // httpEntity = (HttpEntity) httpEntity.getContent();
- // String entityResponse = EntityUtils.toString((HttpEntity) httpEntity.getContent());
- // String entityResponse = EntityUtils.toString((HttpEntity) httpEntity.getContent());
- InputStream entityResponse = httpEntity.getContent();
- // String entityResponseAfterFunctionCall = convertInputStreamIntoString(entityResponse);
- // String entityResponseAfterFunctionCall2 = readFully(entityResponse);
- String entityResponseAfterFunctionCall3 = letsDoThisAgain(entityResponse);
- // String theString = IOUtils.toString(entityResponse);
- Log.e("Entity Response From GetCityDetails Class: ", entityResponseAfterFunctionCall3);
- jsonArray = new JSONArray(entityResponseAfterFunctionCall3);
- // jsonObject=new JSONObject(entityResponse);
- } catch(JSONException e){
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- return jsonArray;
- // return jsonObject;
- }
- // http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
- // http://stackoverflow.com/questions/2492076/android-reading-from-an-input-stream-efficiently/3552721#3552721
- // http://stackoverflow.com/questions/3091359/install-apache-common-lang-2-5-in-eclipse
- // http://stackoverflow.com/questions/179024/adding-a-jar-to-an-eclipse-java-library
- // http://commons.apache.org/proper/commons-io/download_io.cgi
- public String convertInputStreamIntoString(InputStream entityResponse) throws IOException{
- // this method needs the apache commons to work. I downloaded the commons but getting this error
- // java.lang.noclassdeffounderror org.apache.commons.io.ioutils
- // I guess the path directory is messed up?
- StringWriter writer = new StringWriter();
- IOUtils.copy(entityResponse, writer);
- String theString = writer.toString();
- return theString;
- }
- public String readFully(InputStream entityResponse) throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int length = 0;
- while ((length = entityResponse.read(buffer)) != -1) {
- baos.write(buffer, 0, length);
- }
- return baos.toString("UTF-8");
- }
- public String letsDoThisAgain(InputStream entityResponse){
- BufferedInputStream bis = new BufferedInputStream(entityResponse);
- InputStreamReader is = new InputStreamReader(entityResponse);
- StringBuilder sb = new StringBuilder();
- BufferedReader br = new BufferedReader(is);
- try {
- String read = br.readLine();
- while(read !=null){
- sb.append(read);
- read = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement