2014-09-22 48 views
4

我在写函数时遇到问题。我试图将输入流转换为字符串值。我写了两个函数,我试图提取字符串值,但我的Log.e响应返回NULL。以下是我的语法。我究竟做错了什么?由于在Java Android应用程序中将输入流转换为字符串值

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

} 
+1

您只能读取输入流一次,除非它被缓冲,您可以重置它。 – njzk2 2014-09-22 21:33:11

+0

@ njzk2 \t \t'而(bis.available()> 0){ \t \t \t而(读!= NULL){ \t \t \t \t sb.append(读); \t \t \t \t read = br.readLine(); \t \t \t} \t \t \t bis.reset(); '我把它放到我的** letsDoThisAgain **中,现在我得到这个错误**在字符0处输入结束** – bobhope 2014-09-24 14:11:29

+0

'reset'与'mark'一起工作,但您需要检查'isMarkSupported “首先。 – njzk2 2014-09-24 14:24:15

回答

1

readFully通话将使用系统的默认字符编码转换字节的缓冲区转换为字符串。这是不是你想要发生什么。

您应该在toString调用中使用显式字符集。编码通常在HTTP请求头中指定。

下面是如何将UTF-8编码字符串转换

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

你的第二个问题的例子,就是一旦你在readFully通话消耗的InputString,该letsDoThisAgain不会有什么看的,因为InputStream将位于EOF处。

+0

readFully和letDoThisAgain和两个单独的函数。他们没有任何关系 – bobhope 2014-09-24 14:04:30

+0

我总是//读取全文或让我们在运行应用程序时这样做。这两个功能都不依赖于另一个。在运行应用程序时,我至少有一个函数正在运行,但从来没有两个同时运行。 – bobhope 2014-09-24 15:34:13

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