【问题标题】:BufferedReader on Android cutting off my HTTP Post?Android 上的 BufferedReader 切断了我的 HTTP Post?
【发布时间】:2011-09-08 07:19:28
【问题描述】:

使用基本的 Http Post 示例,

try {
    // Construct data
    String data = URLEncoder.encode("param", "UTF-8") + "=" + URLEncoder.encode(param, "UTF-8");
    data += "&" + URLEncoder.encode("param", "UTF-8") + "=" + URLEncoder.encode(param, "UTF-8");
    data += "&" + URLEncoder.encode("param", "UTF-8") + "=" + URLEncoder.encode(param, "UTF-8");
    data += "&" + URLEncoder.encode("param", "UTF-8") + "=" + URLEncoder.encode(param, "UTF-8");

    // Send data
    URL url = new URL("URL THAT IM SEARCHING");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    json = rd.readLine();
    Log.d("TAG",json);
    wr.close();
    rd.close();
} catch (Exception e) { }

我的响应在 android 中被缩短了,而我在 android 之外甚至在我的浏览器中运行它的响应大大缩短了。

android 中的最大响应大小约为 4070 字节,而实际为 14,000+。

我尝试将缓冲区大小设置为 14200,但响应仍然保持不变。

在下面编辑新代码(切换到 https 并尝试了建议的解决方案),仍然得到 ~4070 bye 响应

String getUrl = ("https:.asdadasd/"+trimmed+"?latitude="+lat+"&longitude="+lng+"&distance="+rad);
        Log.d("TAG","URL USED FOR SEARCHING: "+getUrl);
        HttpClient client = new MyHttpClient(getApplicationContext());
        HttpGet get = new HttpGet(getUrl);
        HttpResponse responseGet = null;
        try {
        responseGet = client.execute(get);
        HttpEntity resEntityGet = responseGet.getEntity();    
        InputStream instream = resEntityGet.getContent();

        String result= convertBrToString(instream);
        json = result;
        instream.close();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        Log.d("TAG", json);

        }
  public String convertBrToString(InputStream in) 
  {

          BufferedReader br;
          StringBuffer outString = new StringBuffer();
          br = new BufferedReader(new InputStreamReader(in)); 
          try {
              String read;
              read = br.readLine();
          while(read != null)
          {
              outString.append(read);
              read =br.readLine();
          }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          return outString.toString();     
  }

【问题讨论】:

  • 还有其他人有什么建议吗!?

标签: android http http-post bufferedreader


【解决方案1】:

事实证明,我从来不需要进行转换!一个简单的

    ResponseHandler<String> responseHandler=new BasicResponseHandler();
            String responseBody = client.execute(get, responseHandler);

成功了,但问题是 Log 只会打印出 4096 字节,而且我的 json 解析器得到了错误数组的长度!

感谢大家的支持,记得仔细检查你的代码!

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 2012-03-27
    • 2017-07-08
    相关资源
    最近更新 更多