English 中文(简体)
Anders AsyncTask and HTTP post - Works only one time?
原标题:Android AsyncTask and HTTP post - Works only one time?

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);
最佳回答

这确实感觉到服务器上的问题(第500条编码几乎被普遍用来表明服务器侧代码中存在某种问题,尽管这可能是网络服务器本身的一个问题)。

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?

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签