5

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

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 Dude: ", 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();   
}

}
3
  • 1
    you can read an inputstream only once unless it is buffered and you can reset it. Commented Sep 22, 2014 at 21:33
  • @njzk2 while(bis.available()> 0){ while(read !=null){ sb.append(read); read = br.readLine(); } bis.reset(); } I put that into my letsDoThisAgain and I'm now getting this error End of input at character 0 Commented Sep 24, 2014 at 14:11
  • reset works with mark, but you need to check isMarkSupported first. Commented Sep 24, 2014 at 14:24

2 Answers 2

7
if(inputStream != null)
        {
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();

            String line;
            try {

                br = new BufferedReader(new InputStreamReader(inputStream));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            return sb.toString();
        }
        else
        {
            return "";
        }
Sign up to request clarification or add additional context in comments.

Comments

1

Your readFully call will use your system's default character encoding to transform byte buffer into String. This is NOT what you want to happen.

You should use explicit character set in toString call. The encoding is usually specified in the HTTP request header.

Here is an example of how to convert a UTF-8 encoded string

return baos.toString( "UTF-8" );

Your second problem, is once you've consumed the InputString in readFully call, the letsDoThisAgain will not have anything to read, because the InputStream will be at the EOF.

2 Comments

readFully and letsDoThisAgain and two separate functions. They have nothing to do with one another
I always // readFully or letsDoThisAgain while running the app. Neither function relies on one another. When running the app I at least have one function running but never two at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.