Android AsyncTask 和 HTTP post - 只能运行一次?

发布于 2024-11-03 19:13:21 字数 2166 浏览 1 评论 0原文

我的应用程序中有一个创建 HttpURLConnection 的 AsyncTask。接下来,它调用 getOutputStream(),写入一些字节,刷新并关闭。然后它调用 getResponseCode() 和 getInputStream()。如果我需要发布代码,我可以,但我想我会把问题保持在较小的范围内。

当我第一次运行它时,我得到了 200 响应代码,并且得到了正确的输入流。

当我第二次到第五次运行此程序时,我会得到一个新线程(可在 DDMS 视图中查看),并且在获取输入流时收到 500 响应代码和 IOException。

当我第六次或更多次调用此函数时,不会创建新线程,并且我仍然收到 500 响应代码和 IOException。

我可以在这里寻找什么?它总是有效一次,然后就不再有效。还有其他人看过这个吗?我完全被难住了。

这是最小的代码(我删除了 try/catch、变量声明、应用程序特定的内容等):

   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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2024-11-10 19:13:21

这确实感觉像是服务器上的问题(代码 500 几乎普遍用于指示服务器端代码中的某种问题,尽管它可能是 Web 服务器本身的问题)。

您控制服务器代码吗?您是否可能打开一个文件而不关闭它,以便其他调用可能会遇到“访问被拒绝”或“文件已打开”错误?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文