Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Shane McC
Shane McC
3,005 Points

Convert an inputstream to a string value in Java Android App

I'm having trouble with function(s) I'm writing. I'm trying to convert an inputstream to a string value. I've written two functions and I'm attempting to extract the String value but my Log.e response is returning NULL. Below is my syntax. What am I doing wrong? Thanks

If you can't view my below syntax, please visit http://pastebin.com/5rr6pK6G

    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();
    }


    JSONArray jsonArray = null;
    if(httpEntity !=null){
        try{

            InputStream entityResponse = httpEntity.getContent();
            // not working
            String entityResponseAfterFunctionCall2 = readFully(entityResponse);
            // not working
            String entityResponseAfterFunctionCall3 = letsDoThisAgain(entityResponse);
            Log.e("Entity Response: ", entityResponseAfterFunctionCall3);

            jsonArray = new JSONArray(entityResponseAfterFunctionCall3);

        } catch(JSONException e){
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    return jsonArray;
    }

    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();
    }

    public String letsDoThisAgain(InputStream 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();   
     }

    }

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Do you have access to EntityUtils ? Check out its methods.

The other thing where you are checking httpEntity != null, should probably be looking at:

httpEntity.getContentLength();

This is in case you aren't getting anything back, it should be greater than 0. Your method letsTryThatAgain looks correct. I found this Stack Overflow answer that looks like you are doing the right thing, maybe the content length is 0.

Shane McC
Shane McC
3,005 Points

Hi Craig

My mistake over my delayed response.

I do have access to EntutyUtils but only through a jar file(s) I downloaded via the apache commons. I uploaded these files into my app and I'm getting the below error. I guess my path is messed up? What do you think? Thanks

Caused by: java.lang.NoClassDefFoundError: org.apache.commons.io.IOUtils