how to find server unavailable in android

﹥>﹥吖頭↗ 提交于 2019-12-12 04:48:20

问题


Im developing an app which has download option

Local Server in OnState:-

From My Android Emulator,I Download Audio File and Save it in Distination Path(Local Server), Then I Played the Audio File,it Working Correct, Then My Problem

Local Server in OffState:-

While Downloading Local Server,(192.168.0.2/android/a1.mp3(Local Server Path))But it is OffState,it Still Show Loading Icon.. How Will Find Wheather Server in OffState or Onstate. Is there any delegate functions for connection fail with error


回答1:


What you need here is to setTimeout.

Below code snipper will help you.

HttpPost httpPost = new HttpPost(url);
    StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
    httpPost.setEntity(se);

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 3000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    BasicHttpResponse httpResponse = (BasicHttpResponse)  httpClient.execute(httpPost);

    HttpEntity entity = httpResponse.getEntity();
    return entity;

Here if you dont get response within specific time limit. Code will throw ConnectTimeoutException.



来源:https://stackoverflow.com/questions/10860098/how-to-find-server-unavailable-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!