2014-09-22 62 views
1

我很抱歉,如果這是重複或重複,需要通過建議或httppostHttpURLConnectionsetReadTimeoutsetConnectTimeout通話網址,持續15秒,但問題是我使用的Java 1.4,setReadTimeout()setConnectTimeout()不適用於在java.net的Java 1.4包,任何其​​他替代方式,下面是我的代碼HttpURLConnection類或HttpPost與setTimeout的針對Java 1.4

URL url;             
HttpURLConnection connection = null;      
url = new URL(apiURL);          
connection = (HttpURLConnection)url.openConnection();  
connection.setRequestMethod("POST");      
connection.setRequestProperty("Content-Type", "text/json"); 
connection.setRequestProperty("Content-Length", ""+Integer. 
connection.setRequestProperty("Content-Language", "en-US"); 

我的其他代碼

HttpClient httpClient = new DefaultHttpClient();   
HttpPost postRequest = new HttpPost(apiURL);  
StringEntity input = new StringEntity(jsonBvmMEssage);  
input.setContentType("text/json");      
postRequest.setEntity(input);                     
HttpResponse response = httpClient.execute(postRequest); 

感謝提前

+0

出於好奇,什麼是強迫你使用Java 1.4?這真的,真的很老! – 2014-09-22 06:22:17

+0

因爲客戶端還在使用1.4,將來他們只想升級hava,但新的請求需要實現需要設置超時時間的HttpURLConnection/HttpPost,可以幫我在這個上 – David 2014-09-22 07:01:52

回答

0

終於,我發現了這一點,

HttpPost postRequest = new HttpPost(apiURL);      
HttpParams httpParameters = new BasicHttpParams(); 

int timeoutConnection = 15000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 

int timeoutSocket = 15000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
HttpResponse response = httpClient.execute(postRequest); 

希望這會工作,將測試和更新