Reputation: 1294
I having this code for make a request.
@Override
protected Boolean doInBackground(Object... argList) {
/*
* filling all the variables here..
String method = (String) argList[3];
**/
HttpsURLConnection connection = null;
try {
URL connectURL = new URL(url);
connection = (HttpsURLConnection) connectURL.openConnection();
connection.setRequestProperty("connection", "close");
connection.setRequestMethod(method); // this line
connection.setReadTimeout(10000);
/*
* futher code for process GET AND POST Request.
**/
} catch (IOException e) {
connection.disconnect();
} finally{
connection.disconnect();
}
04-12 19:30:27.147: WARN/System.err(431): java.net.ProtocolException: Connection already established
04-12 19:30:27.419: WARN/System.err(431): at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:429)
04-12 19:30:27.447: WARN/System.err(431): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:127)
04-12 19:30:27.487: WARN/System.err(431): at com.app.services.AppServiceManager.doInBackground(AppServiceManager.java:297)
04-12 19:30:27.526: WARN/System.err(431): at com.app.services.AppServiceManager.doInBackground(AppServiceManager.java:1)
04-12 19:30:27.559: WARN/System.err(431): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-12 19:30:27.617: WARN/System.err(431): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-12 19:30:27.636: WARN/System.err(431): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-12 19:30:27.716: WARN/System.err(431): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-12 19:30:27.751: WARN/System.err(431): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-12 19:30:27.798: WARN/System.err(431): at java.lang.Thread.run(Thread.java:1096)
Upvotes: 1
Views: 2695
Reputation: 4695
IMHO, you'd be better of using the Apache HTTP classes. They just make a lot more sense than the baked in URL stuff that Java has and there's a port directly in the Android API:
http://developer.android.com/reference/org/apache/http/package-summary.html
Then I'd pass that to your AsyncTask as it's superclass:
HttpUriRequest request = new HttpPost(uri):
// ... configure
or
HttpUriRequest request = new HttpGet(uri)
// ... configure
Your AsyncTask would look something like:
new AsyncTask<HttpUriRequest, Void, ResultClass>(){
public ResultClass doInBackground(HttpUriRequest... requests) {
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(requests[0]);
// handle the response
return instanceOfResultClass;
}
// ...
}.execute(request);
You get the idea, hopefully. :)
Upvotes: 2