欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

http连接出错,该如何处理

更新时间: 2014-01-05 02:35:35 责任编辑: Author_N1

 

Http连接出错
在执行到下面时出错,e.getmessage()=null:
Log.i("HttpGet","Get input stream");
InputStreamReader in = new InputStreamReader(conn.getInputStream());
Post方法是在:conn.connect();时出错,也是一样的异常信息。在模拟器中用浏览器打开网址是正常的。


package com.example.httptest;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import android.net.*;
import android.util.Log;

public class MyNetwork {
//HTTP Get Method
public static String HttpGet(String strServerName,String strURI,int port)
{
String strResult = "";
int iPort = port;

//合并URL字符串
String strURL = String.format("http://%1$s:%2$d%3$s", strServerName,iPort,strURI);
Log.i("HttpGet","URL:"+strURL);

URL url = null;
try
{
url = new URL(strURL);
//创建HTTP连接
Log.i("HttpGet","Connection to Server...");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Log.i("HttpGet","Connected");
//获取输入流字符串
Log.i("HttpGet","Get input stream");
InputStreamReader in = new InputStreamReader(conn.getInputStream());
//转换为buffstream
Log.i("HttpGet","Convert to BufferedReader");
BufferedReader bufferread = new BufferedReader(in);
//将接收到的数据转为字符串
String strtemp = null;
Log.i("HttpGet","Bufferread readline");
while((strtemp=bufferread.readLine()) != null)
{
strResult += strtemp;
}
Log.i("HttpGet",strResult);
Log.i("HttpGet","input stream close");
//关闭字符串流
in.close();
//关闭连接
Log.i("HttpGet","Disconnect");
conn.disconnect();
}
catch(SocketException e)
{
Log.i("HttpGet","SocketException:"+e.getMessage());
strResult = String.format("Error:%1$s", e.getMessage());
}
catch(SocketTimeoutException e)
{
Log.i("HttpGet","SocketTimeoutException:"+e.getMessage());
strResult = String.format("Error:%1$s", e.getMessage());
}
catch(FileNotFoundException e)
{
Log.i("HttpGet","FileNotFoundException:"+e.getMessage());
strResult = String.format("Error:%1$s", e.getMessage());
}
catch(IOException e)
{
Log.i("HttpGet","IOException:"+e.getMessage());
strResult = String.format("Error:%1$s", e.getMessage());
}
catch(Exception e)
{
e.printStackTrace();
Log.i("HttpGet","Error:"+e.getMessage());
strResult = String.format("Error:%1$s", e.getMessage());
}

return strResult;
}

//HTTP Post Method
public static String HttpPost(String strServerName,String strURI,String strParams,int port)
{
String strResult = "";
int iPort = port;

//合并URL字符串
String strURL = String.format("http://%1$s:%2$d%3$s", strServerName,iPort,strURI);
Log.i("HttpPost","URL:"+strURL);

URL url = null;
try
{
url = new URL(strURL);
//创建HTTP连接
Log.i("HttpPost","Connection to Server...");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);//设置输入流采用字节流
conn.setDoOutput(true);//设置输出流采用字节流
conn.setRequestMethod("POST");
conn.setUseCaches(false);//不使用缓存
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置参数
conn.setRequestProperty("Charset", "utf-8");
conn.connect();//连接
Log.i("HttpPost","Connected");
//发送参数
Log.i("HttpPost","send params");
DataOutputStream dop = new DataOutputStream(conn.getOutputStream());
dop.writeBytes(strParams);
dop.flush();//发送,清空缓存
dop.close();//关闭
//获取输入流字符串
Log.i("HttpPost","Get input stream");
InputStreamReader in = new InputStreamReader(conn.getInputStream());
//转换为buffstream
Log.i("HttpPost","Convert to BufferedReader");
BufferedReader bufferread = new BufferedReader(in);
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:35:35
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/11862/
本文WWW.DOC100.NET DOC100.NET版权所有。