2
private String getHttpResponse(String lName,String consultancyArea,String fName,String hospitalName,String date,String time){
        String names="a";
        System.out.println("iside httprespons");
            HttpParams httpParams=new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
            HttpConnectionParams.setSoTimeout(httpParams,30000);
            HttpClient httpClient=new DefaultHttpClient(httpParams);
            HttpPost httpPost=new HttpPost("url");

    try{
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(6);
        nameValuePairs.add(new BasicNameValuePair("LName", lName));
        nameValuePairs.add(new BasicNameValuePair("ConsultancyArea", consultancyArea));
        nameValuePairs.add(new BasicNameValuePair("fName", fName));
        nameValuePairs.add(new BasicNameValuePair("hospitalName",hospitalName));
        nameValuePairs.add(new BasicNameValuePair("date", date));
        nameValuePairs.add(new BasicNameValuePair("time", time));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));



        HttpResponse httpResponse=httpClient.execute(httpPost);
        String result=inputStreamToString(httpResponse.getEntity().getContent()).toString();
        JSONObject jasonObejct=new JSONObject(result);
        String name1 = jasonObejct.getString("myData");

        names=name1;


    }catch (JSONException e) {
         System.out.println("network error ocured1");
           e.printStackTrace();

      }catch(ConnectTimeoutException e){
          System.out.println("network error ocured2");
      }
    catch(java.net.SocketTimeoutException e){
          System.out.println("network error ocured2");
          e.printStackTrace();
      }
      catch (ClientProtocolException e) {
          System.out.println("network error ocured2");
       e.printStackTrace();
       System.out.println("network error  ocured");
      } 
      catch (IOException e) {

          System.out.println("network error ocured3");
       e.printStackTrace();
      }
    return names;
}

我正在开发一个 android 应用程序来从远程服务器获取数据。如果服务器没有响应,我想在活动管理器冻结活动之前使连接超时并结束进程。我需要的是一种在 android 活动管理器冻结活动之前结束进程的方法,以防服务器出现故障。

4

1 回答 1

0

使用 AsyncTask,将代码粘贴到 doInBackground 方法中。onPreExecute 和 onPostExecute 方法在 UI 线程上工作,但 doInBackground 创建单独的线程。

于 2013-08-13T16:40:48.443 回答