1

I have the following code for doing a HTTP Post:

InputStream ins = null;
String result = null; 
public String postforBakup(String url){

    java.util.Date date= new java.util.Date();
    Timestamp timestamp = (new Timestamp(date.getTime()));



    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);


    DefaultHttpClient http = new DefaultHttpClient(httpParameters); 
    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

    try{
        JSONArray json = (makeJSON());



        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
        nameValuePairs.add(new BasicNameValuePair("key", processKey()));
        nameValuePairs.add(new BasicNameValuePair("type", "Android"));
        nameValuePairs.add(new BasicNameValuePair("timestamp", timestamp.toString()));
        nameValuePairs.add(new BasicNameValuePair("mail", mail));
        nameValuePairs.add(new BasicNameValuePair("jsonArray", Arrays.asList(json).toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));




        HttpResponse resp = http.execute(httppost); 
        HttpEntity entity = resp.getEntity();
        ins = entity.getContent(); 
        BufferedReader bufread = new BufferedReader(new InputStreamReader(ins, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while((line = bufread.readLine()) != null){
            sb.append(line +"\n"); 
        }
        result = sb.toString().trim(); 
        System.out.println("Response: "+result);




    }catch (Exception e){

        System.out.println("Squish: "+e); 

    }finally{
        try{
            if(ins != null){
                ins.close();
            }
        }catch(Exception smash){
            System.out.println("Squish: "+smash); 
        }
    }
    if(result.equalsIgnoreCase("Authentication failed")){
        System.out.println("Death");
        return "Backup failed"; 
    }else if (result.equalsIgnoreCase("update successfull")){
        System.out.println("Life");
        return "Success";
    }else{
        return "failed";
    }
}

While as the function works properly if connected to WIFI, when I switch to 2g I get the loader for hours without any exception or a message of timeout. What is possibly wrong here?

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Android sets the default timeout to `null/zero` i.e will wait forever, see [here](http://stackoverflow.com/questions/18615213/android-defaulthttpclient-default-timeout) – sled Jan 23 '15 at 04:49
  • But I am using BasicHttpParams - ` HttpParams httpParameters = new BasicHttpParams();` So does the default behavior override the code I have written? – Skynet Jan 23 '15 at 04:50
  • @Skynet: why you are not using `httpurlconnection` instead of `DefaultHttpClient` ? – ρяσѕρєя K Jan 23 '15 at 04:51
  • I did have a HttpUrlConnection function before this - I got mad trying to implement namevalue pairs in that and finally changed to Default Http Connection. – Skynet Jan 23 '15 at 04:53
  • @Skynet: see [Android’s HTTP Clients](http://android-developers.blogspot.in/2011/09/androids-http-clients.html) and use `HttpURLConnection` for better performance. if you are getting any issue to post data using `HttpURLConnection` then share – ρяσѕρєя K Jan 23 '15 at 04:56
  • @Skynet: Ah I'm sorry didn't see that! I'd suggest to use android's [volley](http://developer.android.com/training/volley/index.html) library, much easier to use. – sled Jan 23 '15 at 05:01
  • @ρяσѕρєяK - [Here](http://stackoverflow.com/questions/28061599/understanding-httpurlconnection-how-to-add-namevalue-pairs-to-httpurlconnectio) is a detailed background posted by my colleague – Skynet Jan 23 '15 at 05:02
  • I just got a socket timeout exception - but it is way beyond the time limit I had set - I think it multiplied it by at least 30 times. – Skynet Jan 23 '15 at 05:03

0 Answers0