Tuesday, February 18, 2014

HttpClient set the timeout problem


I tried a variety of methods HttpClient timeout setting has no effect on the problem, I had to set the connection timeout to 5 seconds, read timeout of 5 seconds, with a request to return unsuccessful to 20 seconds, and then I went home to write a demo, try a variety of methods, no effect, seeking God to help me look great, grateful
 package com.example.connectiontimeout; 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpConnectionParams;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private Button button;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
button = (Button) findViewById (R.id.button1);
button.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
new TimeoutAsyncTask () execute ();.
}
});
}

/ / Asynchronous timeout task
public class TimeoutAsyncTask extends AsyncTask {

@ Override
protected String doInBackground (Void. .. voids) {
String result = null;
HttpResponse httpResponse;
BufferedReader br = null;

/ / Website http://www.xiaanming.com/
visit like this one does not existHttpGet httpGet = new HttpGet ("http://www.xiaanming.com/");
HttpClient httpClient = new DefaultHttpClient ();

/ / Method I. This method sets the supermarket invalid to wait 1 minute 20 seconds
httpClient.getParams () setParameter (CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);.
httpClient.getParams () setParameter (CoreConnectionPNames.SO_TIMEOUT, 3000);.

/ / Method two, invalid, or a variety of methods
/ / HttpConnectionParams.setConnectionTimeout (httpClient.getParams (), 3000);
/ / HttpConnectionParams.setSoTimeout (httpClient.getParams (), 2000);

/ /

try {
httpResponse = httpClient.execute (httpGet);
if (httpResponse.getStatusLine (). getStatusCode () == 200) {
br = new BufferedReader (new InputStreamReader (httpResponse.getEntity () getContent ()).);
String line = null;
while ((line = br.readLine ())! = null) {
result = result + line;
}
}
} Catch (ClientProtocolException e) {
e.printStackTrace ();
} Catch (IOException e) {
e.printStackTrace ();
} Finally {
try {
if (br! = null) {
br.close ();
br = null;
}
} Catch (IOException e) {
e.printStackTrace ();
}

httpClient.getConnectionManager () shutdown ();.
}

return result;
}
}


}

Code is above it, a button, click on the request network, http://www.xiaanming.com is a domain name that does not exist<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Great God where are you? How did a man Reply
Reply:
Give yourself Dingding ah
Reply:
Great God, Check it out
Reply:
I tested the ah, the following code is valid:
 HttpConnectionParams.setConnectionTimeout (httpClient.getParams (), 15000); 
HttpConnectionParams.setSoTimeout (httpClient.getParams (), 10000);


Reply:
references, 4th Floor tcmakebest reply:
I tested ah, the following code is valid:


Java code
?



12

HttpConnectionParams.setConnectionTimeout (httpClient.getParams (), 15000); HttpConnectionParams.setSoTimeout (httpClient.getParams (), 10000);
Are you sure effective? How do I have to wait a minute it
Reply:
 HttpParams params = httpClient.getParams (); 

The parameters extracted, assignment, and then set it back. Try this!
Reply:
The landlord, how to solve ah, I also encountered the same problem
Reply:
The landlord did not solve this problem?

Reply:
I also encountered the same problem, how to solve ah?
Reply:
We now set 3000, at a lot of connection timeout, we adjusted to 6000, and is testing results.

No comments:

Post a Comment