Android 上的 BufferedReader 会切断我的 HTTP Post?
使用基本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我从来不需要进行转换!一个简单的
方法就解决了,但问题是 Log 只会打印出 4096 字节,而且我的 json 解析器得到了错误数组的长度!
感谢大家的支持,&请记住仔细检查您的代码!
Turns out, I never had to do the conversion! a simple
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!