Android 上的 BufferedReader 会切断我的 HTTP Post?

发布于 2024-12-03 06:55:36 字数 2649 浏览 0 评论 0原文

使用基本的 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();     
  }

Using a basic Http Post example,

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) { }

My response is shortened in android vs the response that I would get running it outside android or even in my browser greatly.

The size of the max response within android is ~4070 bytes vs its actual 14,000+.

I've tried setting my buffer size to 14200 and the response has still stayed the same.

Edit new code below (switched to https & tried suggested solution), still getting ~4070 bye response

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燕归巢 2024-12-10 06:55:36

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

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

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

感谢大家的支持,&请记住仔细检查您的代码!

Turns out, I never had to do the conversion! a simple

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

Did the trick, but the problem is that Log will only print out 4096bytes, and that my json parser was getting the length of the wrong array!

Thank you for all your support everyone, & remember to double check your code!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文