Android BufferedInputStream HTTP POST/GET
我使用 BufferedInputStream 进行 HTTP POST/GET
但出现以下错误
- java.io.FileNotFoundException: http://XX.XX.XX.XX/WebWS/data.aspx
- 传输端点未连接
为什么会出现此错误。我的代码如下,
URL url = new URL(glob.postUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
try {
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpConn.setRequestProperty("Content-Language", "TR");
httpConn.setConnectTimeout(12000);
Iterator<String> reqProps = hMap.keySet().iterator();
while (reqProps.hasNext()) {
String key = reqProps.next();
String value = hMap.get(key);
httpConn.addRequestProperty(key, value);
}
InputStream in = new BufferedInputStream(httpConn.getInputStream());
StringBuilder builder = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} finally {
in.close();
}
httpConn.disconnect();
谢谢。
I Use BufferedInputStream For HTTP POST/GET
But I Get Some Error the Below
- java.io.FileNotFoundException: http://XX.XX.XX.XX/WebWS/data.aspx
- Transport endpoint is not connected
Why Get This Error. My Code is Below
URL url = new URL(glob.postUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
try {
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpConn.setRequestProperty("Content-Language", "TR");
httpConn.setConnectTimeout(12000);
Iterator<String> reqProps = hMap.keySet().iterator();
while (reqProps.hasNext()) {
String key = reqProps.next();
String value = hMap.get(key);
httpConn.addRequestProperty(key, value);
}
InputStream in = new BufferedInputStream(httpConn.getInputStream());
StringBuilder builder = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} finally {
in.close();
}
httpConn.disconnect();
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否有任何原因不使用
HttpClient
?
您可以将代码替换为以下内容:
您可以使用 ClientConnectionManager 和 HttpParams 设置 HttpClient 以确保安全性,并在初始化时为客户端设置各种 http 参数(如果您搜索类名,则会有大量示例)。
Is there any reason you're not using
HttpClient
?You can replace your code with something like:
You can setup the HttpClient with ClientConnectionManager and HttpParams for security and various http parameters for the client at initialisation (plenty of examples around if you search on class names).
如果 HTTP 响应状态代码为 400 或更高(即服务器端出现任何错误情况),
HttpURLConnectionImpl.getInputStream()
会抛出FileNotFoundException
。您应该检查状态代码到底是什么,以获得合适的调试信息。不过,我赞同 Mark Fisher 关于使用 HttpClient 的建议,据我所知,这是在 Android 上使用 HTTP 的首选方式。
HttpURLConnectionImpl.getInputStream()
is known to throw aFileNotFoundException
if the HTTP response status code is 400 or higher, i.e. for any error condition on the server side. You should check what the status code really is in order to obtain suitable debug information.However, I second Mark Fisher's suggestion about using HttpClient, which AFAIK is the preferred way of working with HTTP on Android.