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.
在我第二次到第五次开会时,我有了新的透镜(见DDMS观点),我收到500份回复守则,在获得投入时收到一份综合观察文件。
当我第六次或多次打电话时,没有建立新的透镜,我仍然收到500份回复守则和《综合观察》。
我可以在这里谈谈什么? 它总是一劳永逸地工作。 是否有其他人看到这一点? I m完全瘫痪。
这里的《MINIMAL法典》(我删除了审判/捕获物、变式申报、具体 st子等):
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);