Convertir un inputtream en un valor de cadena en Java Android App

Estoy teniendo problemas con las funciones que estoy escribiendo. Estoy tratando de convertir un inputtream a un valor de cadena. He escrito dos funciones y estoy tratando de extraer el valor de la cadena, pero mi respuesta Log.e está devolviendo NULL. A continuación se muestra mi sintaxis. ¿Qué estoy haciendo mal? Gracias

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

 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 ""; } 

Su llamada readFully utilizará la codificación de caracteres predeterminada de su sistema para transformar el búfer de bytes en String. Esto NO es lo que quieres que suceda.

Debe utilizar un conjunto de caracteres explícito en la llamada toString . La codificación se especifica normalmente en el encabezado de solicitud HTTP.

Aquí hay un ejemplo de cómo convertir una cadena UTF-8 codificada

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

Su segundo problema, es que una vez que haya consumido el InputString en readFully llamada, el letsDoThisAgain no tendrá nada que leer, porque el InputStream estará en el EOF.

  • ¿Cómo integrar paytm wallet en la aplicación android?
  • Convertir timestamp en fecha actual en android
  • Cómo almacenar Hashmap en Android para que se reutilice cuando la aplicación se reinicie con las preferencias compartidas?
  • Los archivos múltiples dex definen <mi paquete> / BuildConfig, no pueden encontrar la causa:
  • Com.tools.fd.runtime.BootstrapApplication no se ejecutará a menos que actualice los servicios de Google Play
  • Android: ¿Cómo encontrar el ancho y la altura de la pantalla?
  • La selección de proyectos en configuraciones de ejecución en Eclipse está vacía
  • La aplicación no puede cargar anuncios intersticiales en mi dispositivo real
  • Precisamente Sync Looped Audio con animación en Android
  • Intento pendiente siempre hacer nueva actividad
  • Una pregunta muy abierta, n00b-esque acerca de extender el tutorial de Bloc de notas en Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.