Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - HttpClient only works once

I'm opening an url in the background because when going to the link it 'likes' the article 3020

I'm doing this with HttpClient:

public static class LoadURL extends AsyncTask<Void, Integer, Void>{

              @Override
              protected Void doInBackground(Void... params) {
                    try {
                second_client = new DefaultHttpClient();  
                String getURL = "http://website.com/" + id_string + "/like/";
                HttpGet get = new HttpGet(getURL);
                HttpResponse responseGet = second_client.execute(get, cookieStuff);

              Log.i("Response", String.valueOf(response.getStatusLine().getStatusCode()));  

            } catch (Exception e) {
                e.printStackTrace();
            }
               return null;

              }

              @Override
              protected void onPostExecute(Void result) {
               super.onPostExecute(result);
               Toast.makeText(AFragment.context, "Article liked", Toast.LENGTH_LONG).show();  
              }
        }

The problem is that there are plenty of articles (3020-3021-3022 etc.) so the user will click multiple times the 'like' button. That means the LoadURL task is done multiple times but it only works the first time. All times after that it takes like 5+ minutes.

Does anybody know how to solve this? Or is there a better way than using HttpClient?

like image 397
GromDroid Avatar asked Jan 23 '26 06:01

GromDroid


1 Answers

Are you sure the web server is working well? 1. First thing you should do is create a static instance of DefaultHttpsClient and use the same one every time, it should work well, I've used it a lot of times. 2. Take a look at volley, it is a new library google has released for networking

like image 147
bogdan Avatar answered Jan 24 '26 22:01

bogdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!