简体   繁体   中英

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?

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

You need to instantiate client only once and reuse it using ThreadedClientConnManager

HttpClient mClient;
 HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params, 100);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme ("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

        mClient = new DefaultHttpClient(cm, params);

Now just reuse client

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

              @Override
              protected Void doInBackground(Void... params) {
                    try {

                String getURL = "http://website.com/" + id_string + "/like/";
                HttpGet get = new HttpGet(getURL);
                HttpResponse responseGet = mClient.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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM