Reputation: 5924
I have an AsyncTask in my application that creates a HttpURLConnection. Next, it calls getOutputStream(), writes some bytes, flushes and closes. Then it calls getResponseCode(), and getInputStream(). If I need to post code, I can, but thought I'd keep the question small.
When I run this the first time, I get a 200 response code, and I get the correct input stream.
When I run this a second to fifth time, I get a new thread (viewable in the DDMS view), and I receive a 500 response code, and an IOException when getting the input stream.
When I call this a sixth time or more, no new threads are created, and I still get the 500 response code and the IOException.
What can I look for here? It always works one time and never again. Has anyone else seen this? I'm completely stumped.
Here's MINIMAL code (I removed try/catch, variable declarations, app specific stuff, etc):
protected String doInBackground(String... params)
{
connectURL = new URL(sWebPath);
conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "MyAppAgent");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
// Setup my post as a string s.
conn.setRequestProperty("Content-Length", String.valueOf(s.length()));
conn.connect();
DataOutputStream dataStream = new DataOutputStream(conn.getOutputStream());
dataStream.writeBytes(s);
dataStream.flush();
dataStream.close();
dataStream = null;
// 200 1st time only, 500 after that.
responseCode = conn.getResponseCode();
// Works 1st time only, IO error after that
DataInputStream dis = new DataInputStream(conn.getInputStream());
byte[] data = new byte[16384];
int len = dis.read(data, 0, 16384);
dis.close();
conn.disconnect();
response = new String(data, 0, len);
// do whatever with my response
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
// Now I call a Toast message on the original context used to
// create this AsyncTask.
}
// The onClickListener of a button calls this AsyncTask (TierRequest class) with only two lines
TierRequest t = new TierRequest(WhateverMyCurrentActivityIs.this);
t.execute(A_Constant_Indicating_The_Type_Of_Post);
Upvotes: 1
Views: 1459
Reputation: 7850
This really feels like a problem on the server (code 500 is almost universally used to indicate some sort of problem in the server side code, although it could be a problem with the web server itself).
Do you control the server code? Are you possibly opening a file and not closing it, so that additional calls may run into "access denied" or "file already open" errors?
Upvotes: 1