Question

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;
}

I'm developing an android application to get data from a remote server. I want to time out the connection and end the process before the activity manager freezes the activity if the server didn't respond. What i need is a method to end the process before android activity manager freeze the activity in case of server fault.

Was it helpful?

Solution

Use an AsyncTask, paste your code into doInBackground Method. onPreExecute and onPostExecute methods working on UI Thread but doInBackground create separate thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top