android客户端 长连接超时,Android java.net.SocketTimeoutException:连接超时

最近,我在程序中遇到以下错误:

11-18 12:30:30.259: W/System.err(21368): java.net.SocketTimeoutException: Connection timed out

11-18 12:30:30.259: W/System.err(21368): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)

11-18 12:30:30.259: W/System.err(21368): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)

11-18 12:30:30.259: W/System.err(21368): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)

11-18 12:30:30.259: W/System.err(21368): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)

11-18 12:30:30.259: W/System.err(21368): at java.net.Socket.connect(Socket.java:1002)

11-18 12:30:30.259: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:75)

11-18 12:30:30.259: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:48)

11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)

11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)

11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)

11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)

11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)

11-18 12:30:30.269: W/System.err(21368): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)

11-18 12:30:30.269: W/System.err(21368): at com.example.simplevider.SimpleVideo$4.run(SimpleVideo.java:122)

11-18 12:30:30.279: W/System.err(21368): at java.lang.Thread.run(Thread.java:1019)

产生此错误的函数如下:

private void sendStuff() {

Log.e("sendStuff", "======================================");

new Thread(new Runnable() {

@Override

public void run() {

final int BUFFER_SIZE = 4096;

try {

File uploadFile = new File(existingFileName);

System.out.println("File to upload: " + existingFileName);

URL url = new URL(URL);

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

httpConn.setUseCaches(false);

httpConn.setDoOutput(true);

httpConn.setRequestMethod("POST");

httpConn.setReadTimeout(60*1000);

httpConn.setConnectTimeout(60 * 1000);

// sets file name as a HTTP header

httpConn.setRequestProperty("fileName", uploadFile.getName());

httpConn.setRequestProperty("extra-id", uploadFile.getAbsoluteFile().toString());

httpConn.setRequestProperty("extra-id2", uploadFile.getParent());

httpConn.setRequestProperty("extra-id3", uploadFile.length() + "");

OutputStream outputStream = httpConn.getOutputStream(); //<< this is the source of the error

FileInputStream inputStream = new FileInputStream(uploadFile);

byte[] buffer = new byte[BUFFER_SIZE];

int bytesRead = -1;

System.out.println("Start writing data...");

while ((bytesRead = inputStream.read(buffer)) != -1) {

outputStream.write(buffer, 0, bytesRead);

}

System.out.println("Data was written.");

outputStream.close();

inputStream.close();

// always check HTTP response code from server

int responseCode = httpConn.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

// reads server's response

BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));

String response = reader.readLine();

System.out.println("Server's response: " + response);

} else {

System.out.println("Server returned non-OK code: " + responseCode);

}

} catch (Exception e) {

e.printStackTrace();

sendStuff();

}

}

}).start();

}

此函数在正常的JAVA中可以正常使用,但是当复制到Android时,它会抛出java.net.SocketTimeoutException:连接超时,我不知道为什么.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款由Google开发的集成开发环境(IDE),用于开发Android应用程序。它提供了丰富的工具和功能,帮助开发者创建、调试和测试Android应用。 关于你提到的错误信息"java.net.SocketTimeoutException: Connect timed out",这是一个网络连接超时的异常。它通常表示在尝试建立网络连接时,连接花费的时间超过了预设的超时时间。 这个错误可能有多种原因,包括但不限于以下几点: 1. 网络连接问题:可能是由于网络不稳定或者网络设置问题导致的连接超时。你可以尝试检查你的网络连接是否正常,并且确保你的网络设置正确。 2. 防火墙或代理问题:如果你使用了防火墙或代理服务器,可能会导致连接超时。你可以尝试关闭防火墙或者检查代理设置是否正确。 3. 服务器问题:有时候服务器可能出现故障或者负载过高,导致连接超时。你可以尝试等待一段时间后再次尝试连接。 如果你遇到了这个错误,可以尝试以下几个解决方法: 1. 检查网络连接是否正常,并确保网络设置正确。 2. 关闭防火墙或者检查代理设置是否正确。 3. 尝试使用其他网络环境,比如切换到其他的Wi-Fi网络或者使用移动数据网络。 4. 等待一段时间后再次尝试连接,有时候服务器问题会自行解决。 希望以上信息对你有帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值