简体   繁体   English

java.net.Socket不要在Android上抛出IOException

[英]java.net.Socket don't throw IOException at Android

  • What I DO : 我所做的 :

    Write an upload program using java.net.Socket at Android Mobile 在Android Mobile上使用java.net.Socket编写上传程序

  • What I WANT: 我想要的是:

    Connect the Server, When IOException happens(such as the bad network state,no network etc.),try to connect the server three times 连接服务器,当发生IOException(例如网络状态错误,无网络等)时,请尝试连接服务器三遍

  • What The Question: 什么问题:

    Close the WIFI or Mobile GPRS and just run it, No IOexception happened and APP just stop at this point long long time. 关闭WIFI或移动GPRS并运行它,没有IOexception发生,APP长时间就此停止了。

  • Just See My Code: 只看我的代码:

     try { do { socket = new Socket(); SocketAddress socketAddress = new InetSocketAddress(API.UPLOAD_SERVER, API.UPLOAD_PORT); socket.setPerformancePreferences(1, 0,0); socket.setSoTimeout(1000); socket.connect(socketAddress, 2000); attempt = 3; } while (attempt < 3); mListener.onStart(); } catch (SocketException e) { Log.e("UploadTask","exception"); return; } catch (UnknownHostException e) { return; } catch (IOException ioe) { try { Thread.sleep(100); } catch (InterruptedException e) { return ; } attempt++; if (attempt == 3) { return; } } 
  • Help Me or ...: I hope someone can help me,I will wait your answer online, Thanks. 帮我还是...:希望有人可以帮助我,我将在线等待您的回答,谢谢。

you have nothing output or exception if you close WIFI or GPRS. 如果您关闭WIFI或GPRS,则没有任何输出或异常。 Does this mean you app will run over or you code will stay in one sentence,for example, the "connect" method? 这是否意味着您的应用程序将运行或您的代码将停留在一个句子中,例如“ connect”方法?

I have a suggestion. 我有个建议。 If you want to explore the API of android, that's OK. 如果您想探索android的API,那就可以了。 OR 要么

Maybe you can test Connectivity before your socket connect. 也许您可以在套接字连接之前测试连通性。

private boolean isOpenNetwork() {  
    ConnectivityManager connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  
    if(connManager.getActiveNetworkInfo() != null) {  
        return connManager.getActiveNetworkInfo().isAvailable();  
    }  

    return false;  
}

hope this can help.. 希望这可以帮助..

PS: I have no idea about the direct solution to your question, for I have no idea about the socket realize detail. PS:我不知道直接解决您的问题,因为我不知道套接字实现细节。 Maybe someone else can explain it. 也许其他人可以解释它。

Assuming that the "connect" method succeeds (which can happen), the API is build in a way that you get IOException(s) when actually reading/writing to the streams of the socket. 假设“ connect”方法成功(可能发生),则以实际读取/写入套接字流的方式获得IOException的方式构建API。

socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(API.UPLOAD_SERVER, API.UPLOAD_PORT);
socket.setPerformancePreferences(1, 0,0);
socket.setSoTimeout(1000);
socket.connect(socketAddress, 2000);

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

// Now for example, if you want to read some data
int data;
try( data = is.read() ){
   // Usual processing
} catch( IOException ){
   // Disconnected... try reconnecting etc.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM