使用基本的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再见
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();
}
答案 0 :(得分:0)
原来,我从来没有做过转换!一个简单的
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = client.execute(get, responseHandler);
诀窍,但问题是Log只打印出4096bytes,而且我的json解析器得到了错误数组的长度!
感谢大家的支持,&amp;记得仔细检查你的代码!