मैं निम्न समस्या से जूझ रहा हूं: मेरा ऐप एचटीपी क्लाइंट का उपयोग कर http सर्वर के अनुरोधों का अनुक्रम बनाता है। मैं सर्वर पर डेटा भेजने के लिए HttpPut का उपयोग करता हूं। पहला अनुरोध अच्छी तरह से और तेज़ हो जाता है, दूसरा अनुरोध 40 सेकंड के लिए लटकता है और फिर मैं कनेक्शन टाइम अपवाद को पकड़ता हूं। मैं अपने एचटीपी क्लाइंट का पुन: उपयोग करने की कोशिश कर रहा हूं और उसी उदाहरण के माध्यम से दूसरा अनुरोध भेज रहा हूं। यदि मैं नए कनेक्शन प्रबंधक के साथ एक साथ नया HttpClient बना देता हूं, तो सबकुछ ठीक काम करता है।एंड्रॉइड httpclient सर्वर के दूसरे अनुरोध पर लटकता है (कनेक्शन का समय समाप्त हो गया है)
ऐसा क्यों हो रहा है? और इसे कैसे ठीक करें और हर बार नया HttpClient न बनाएं?
अग्रिम धन्यवाद। अगर मैं टिप्पणी readClient = newHttpClient (readClient) doPut में, तो समस्या पैदा होती है (
public class WebTest
{
private HttpClient readClient;
private SchemeRegistry httpreg;
private HttpParams params;
private URI url; //http://my_site.net/data/
protected HttpClient newHttpClient(HttpClient oldClient)
{
if(oldClient != null)
oldClient.getConnectionManager().shutdown();
ClientConnectionManager cm = new SingleClientConnManager(params, httpreg);
return new DefaultHttpClient(cm, params);
}
protected String doPut(String data)
{
//****************************
//Every time we need to send data, we do new connection
//with new ConnectionManager and close old one
readClient = newHttpClient(readClient);
//*****************************
String responseS = null;
HttpPut put = new HttpPut(url);
try
{
HttpEntity entity = new StringEntity(data, "UTF-8");
put.setEntity(entity);
put.setHeader("Content-Type", "application/json; charset=utf-8");
put.setHeader("Accept", "application/json");
put.setHeader("User-Agent", "Apache-HttpClient/WebTest");
responseS = readClient.execute(put, responseHandler);
}
catch(IOException exc)
{
//error handling here
}
return responseS;
}
public WebTest()
{
httpreg = new SchemeRegistry();
Scheme sch = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
httpreg.register(sch);
params = new BasicHttpParams();
ConnPerRoute perRoute = new ConnPerRouteBean(10);
ConnManagerParams.setMaxConnectionsPerRoute(params, perRoute);
ConnManagerParams.setMaxTotalConnections(params, 50);
ConnManagerParams.setTimeout(params, 15000);
int timeoutConnection = 15000;
HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 40000;
HttpConnectionParams.setSoTimeout(params, timeoutSocket);
}
private ResponseHandler<String> responseHandler = new ResponseHandler<String>()
{
@Override
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException
{
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300)
{
throw new HttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
if(entity == null)
return null;
InputStream instream = entity.getContent();
return this.toString(entity, instream, "UTF-8");
}
public String toString(
final HttpEntity entity,
final InputStream instream,
final String defaultCharset) throws IOException, ParseException
{
if (entity == null)
{
throw new IllegalArgumentException("HTTP entity may not be null");
}
if (instream == null)
{
return null;
}
if (entity.getContentLength() > Integer.MAX_VALUE)
{
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
int i = (int)entity.getContentLength();
if (i < 0)
{
i = 4096;
}
String charset = EntityUtils.getContentCharSet(entity);
if (charset == null)
{
charset = defaultCharset;
}
if (charset == null)
{
charset = HTTP.DEFAULT_CONTENT_CHARSET;
}
Reader reader = new InputStreamReader(instream, charset);
StringBuilder buffer=new StringBuilder(i);
try
{
char[] tmp = new char[1024];
int l;
while((l = reader.read(tmp)) != -1)
{
buffer.append(tmp, 0, l);
}
} finally
{
reader.close();
}
return buffer.toString();
}
};
}
सर्वर अपने कनेक्शन को बंद किया जा सकता है। प्रतिक्रिया शीर्षलेख क्या हैं? –
सर्वर को मेरा दूसरा अनुरोध भी प्राप्त नहीं होता –
उपभोग सामग्री() उत्तर है, –