Created
August 9, 2012 17:31
-
-
Save szepeshazi/3306225 to your computer and use it in GitHub Desktop.
HttpUrlConnection disconnect issue on HTC (Froyo and below)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.wamped.playground; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.widget.TextView; | |
public class HttpTestActivity extends Activity { | |
private static final String TAG = "HTTP test"; | |
private static final String testURL = "http://naugye.notnoise.org/ping.php"; | |
protected static final int gracePeriod = 10 * 1000; | |
private static PollThread pollThread; | |
private TextView tv; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.soap_test); | |
tv = (TextView) findViewById(R.id.soapview_main); | |
pollThread = new PollThread(1, 5); | |
pollThread.start(); | |
Log.d(TAG, "pollThread started"); | |
tv.append("pollThread started"); | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
pollThread.cancelRequest(); | |
Log.d(TAG, "pollThread presumably cancelled"); | |
tv.append("pollThread presumably cancelled"); | |
} | |
}, 1000); | |
} | |
private class PollThread extends Thread { | |
private int attempts; | |
private int responseTimeout; | |
private HttpURLConnection connection; | |
private static final String TAG = "PollThread"; | |
private String msg; | |
public PollThread(int attempts, int responseTimeout) { | |
super(); | |
this.attempts = attempts; | |
this.responseTimeout = responseTimeout; | |
} | |
@Override | |
public void run() { | |
Log.d("PollThread", "run() called"); | |
long stopWatch = 0; | |
int attemptCount = 0; | |
BufferedReader reader = null; | |
while (attemptCount++ < attempts) { | |
try { | |
stopWatch = System.currentTimeMillis(); | |
Log.d(TAG, "Initiating new request with " + String.valueOf((gracePeriod / 1000) + responseTimeout) + " seconds timeout."); | |
String requestURL = prepareRequestURL(attemptCount); | |
Log.d(TAG, "Request URL is " + requestURL); | |
URL url = new URL(requestURL); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestProperty("Accept-Charset", "UTF-8"); | |
connection.setConnectTimeout(5 * 1000); | |
connection.setReadTimeout((responseTimeout * 1000) + gracePeriod); | |
connection.setRequestMethod("GET"); | |
// read the output from the server | |
reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
stringBuilder.append(line + "\n"); | |
} | |
Log.d(TAG, "Received response after " + String.valueOf((System.currentTimeMillis() - stopWatch) / 1000) + " seconds"); | |
msg = "Response: " + stringBuilder; | |
Log.d(TAG, msg); | |
/* | |
runOnUiThread(new Runnable() { | |
public void run() { | |
tv.append(msg); | |
} | |
}); | |
*/ | |
} catch (IOException ioe) { | |
msg = "IOException after " + String.valueOf((System.currentTimeMillis() - stopWatch) / 1000) + " seconds, " + ioe; | |
Log.e(TAG, msg); | |
/* | |
runOnUiThread(new Runnable() { | |
public void run() { | |
tv.append(msg); | |
} | |
}); | |
*/ | |
} finally { | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
} | |
} | |
} | |
Log.d(TAG, "Exiting poll thread."); | |
} | |
public void cancelRequest() { | |
if (connection != null) { | |
connection.disconnect(); | |
} | |
} | |
private String prepareRequestURL(int attemptCount) { | |
String requestURL = testURL; | |
String charset = "UTF-8"; | |
try { | |
requestURL = requestURL | |
+ "?" | |
+ String.format("timeout=%s&message_id=%s", URLEncoder.encode(String.valueOf(responseTimeout), charset), | |
URLEncoder.encode(String.valueOf(attemptCount), charset)); | |
} catch (UnsupportedEncodingException e) { | |
Log.e("TAG", "prepareRequest() " + e); | |
} | |
return requestURL; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment