Here is my code with AndroidHttpClient:
final AndroidHttpClient httpclient =
AndroidHttpClient.newInstance(getUserAgent(context));
try {
final HttpGet httpGet = new HttpGet(query.getUrl());
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 30000);
HttpConnectionParams.setSoTimeout(httpclient.getParams(), 60000);
// Execute HTTP Get Request
final HttpResponse response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try {
response.getEntity().writeTo(outstream);
outstream.flush();
return outstream.toByteArray();
} finally {
outstream.close();
}
final InputStream is = response.getEntity().getContent();
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
int nRead;
final byte[] data = new byte[255];
while (dis)
while ((nRead = is.read(data, 0, data.length)) != -1) {
bos.write(data, 0, nRead);
}
bos.flush();
return bos.toByteArray();
} finally {
is.close();
bos.close();
}
} else {
throw new BetaSeriesHttpException(context);
}
} catch (final IllegalArgumentException e) {
Log.e(TAG, String.format("Query URL malformed: %s - %s", query, e));
throw new BetaSeriesHttpException(context);
} catch (final IOException e) {
Log.e(TAG, String.format("Impossible to read data from query URL: %s - %s",
query, e), e);
throw new BetaSeriesHttpException(context);
} finally {
httpclient.close();
}
And my code with HttpURLConnection:
try {
final URL url = new URL(query.getUrl());
final URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", getUserAgent(context));
System.setProperty("http.agent", "");
final HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
final InputStream is = httpConnection.getInputStream();
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
int nRead;
final byte[] data = new byte[1024];
while ((nRead = is.read(data, 0, data.length)) != -1) {
bos.write(data, 0, nRead);
}
bos.flush();
return bos.toByteArray();
} finally {
is.close();
bos.close();
}
} else {
throw new BetaSeriesHttpException(context);
}
} catch (final MalformedURLException e) {
Log.e(TAG, String.format("Query URL malformed: %s - %s", query, e));
throw new BetaSeriesHttpException(context);
} catch (final IOException e) {
Log.e(TAG, String.format("Impossible to read data from query URL: %s - %s",
query, e));
throw new BetaSeriesHttpException(context);
}
On Tuesday, November 6, 2012 3:31:49 PM UTC+1, Nikolay Elenkov wrote:
>
> On Tue, Nov 6, 2012 at 11:21 PM, Thomas Bouron
> <[email protected]<javascript:>>
> wrote:
> > I know and I did. But as I say, even if I increase the timeout (up to 1
> > minute) I got the exact same issue.
>
> Show some code.
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en