java - 在 Java Android App 中将输入流转换为字符串值

标签 java android

我在编写函数时遇到问题。我正在尝试将输入流转换为字符串值。我写了两个函数,我试图提取字符串值,但我的 Log.e 响应返回 NULL。下面是我的语法。我究竟做错了什么?谢谢

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

关于java - 在 Java Android App 中将输入流转换为字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25983425/

相关文章:

java - 为什么我的类不能实现在其中声明的接口(interface)?

android - 用 setImageBitmap 替换对 setImageResource() 的调用以减少打嗝?

android - 如何在不考虑时间的情况下从 unixtime(integer) 列包含当前日期的表中选择记录?

android - 在 Android 中与 ORB 匹配时出错

java - viewpager 中的 fragment ,未发现 View 错误

JavaFX 进度条从单独的函数更改

javascript - Phonegap Android screen.height 和 screen.width 返回黑色系统栏空间

android - 将 BottomSheetDialogFragment 高度设置为全屏

执行过程时 JavaFX 不确定进度条

java - 如何使用 Java 从可变 Json 文件创建 Sql 语句