如何在Inputstream Reader中更改缓冲区大小?

时间:2011-08-29 06:55:54

标签: android web-services

我正在使用WebService获取数据,但我只获得了62条记录,我认为这是缓冲区大小的问题,我是对的吗?

我使用以下代码从WebService获取数据,而Webserivce是在php中开发的。

public JSONObject PostConnectionObject(String strUrl,ArrayList<NameValuePair> alstNameValuePair) {
        InputStream mInputStream = null;
        try {
            //This is the default apacheconnection.
            HttpClient mHttpClient = new DefaultHttpClient();

            //Pathe of serverside 
            HttpPost mHttpPost = new HttpPost(strUrl);
            if(alstNameValuePair!=null)
            { 
                //post the valur you want to pass.
                 mHttpPost.setEntity(new UrlEncodedFormEntity(alstNameValuePair));
            }

            //get the valu from the saerverside as response.
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            mInputStream = mHttpEntity.getContent();

          } 
          catch (Exception e) {
             // TODO Auto-generated catch block
             Log.e(strTAG,"Error in HttpClient,HttpPost,HttpResponse,HttpEntity");
          }

         String strLine = null;
         String strResult = null;

        //convert response in to the string.
        try {
              BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(mInputStream,"iso-8859-1"), 8);
              StringBuilder mStringBuilder = new StringBuilder();
              while((strLine = mBufferedReader.readLine()) != null) {
                mStringBuilder.append(strLine + "\n");
              }
              mInputStream.close();
            strResult = mStringBuilder.toString();
            System.out.println("Value of Result :"+strResult);
           } 
           catch (Exception e) {
              // TODO Auto-generated catch block
              //System.out.println("Error in BufferedReadering");
              Log.e(strTAG,"Error in BufferedReadering");
            }

1 个答案:

答案 0 :(得分:1)

InputStreamReader的文档没有提供设置它使用的缓冲区大小的方法。缓冲区的大小取决于实现者。

在代码中,BufferedReader()的缓冲区大小为8。因此,您可以通过更改8(BufferedReader()的第二个参数)来更改BufferedReader缓冲区的大小。

我认为这不会有所帮助。即使缓冲区大小远小于行的长度,BufferedReader仍会返回整行。

您的代码看起来很直接,我怀疑数据永远不会被发送。