2012-04-29 33 views
7

Ice Cream Sandwichに更新されて以来、私のPOST要求はもう機能しません。 ICSの前に、これは正常に動作します:私はHttpURLConnectionは、setDoOutput(true)およびsetRequestMethod( "POST")にもかかわらず、POST要求の代わりに常にGET要求を実行します。

connection.setDoOutput(true); 

を設定しようとしたが、それは作品をしない

try { 
     final URL url = new URL("http://example.com/api/user"); 
     final HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setDoOutput(false); 
     connection.setDoInput(true); 
     connection.setRequestProperty("Content-Length", "0"); 
     if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
      Log.w(RestUploader.class.getSimpleName(), ": response code: " + connection.getResponseMessage()); 
     } else { 
      final BufferedReader reader = new BufferedReader(
        new InputStreamReader(connection.getInputStream())); 
      final String line = reader.readLine(); 
      reader.close(); 
      return Long.parseLong(line); 
     } 
    } catch (final MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (final ProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (final IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return -1; 

。サーバの応答は常に405(Method not allowed)であり、サーバログはGET要求であると言います。

setRequestMethodへのAndroidのJavaDocは言う:

接続が行われる前に、このメソッドのみ呼び出すことができます。

url.openConnection()の前にメソッドを呼び出さなければならないということですか? HttpURLConnectionインスタンスの作成方法は?私が見たすべての例は、上記のようにします。

誰かがなぜPOSTの代わりにGETリクエストを送信するのかというアイデアがあることを願っています。

ありがとうございます。上記の二つの文で

+0

UIスレッドでHttpRequestを使用していますか? –

+0

いいえ、私は '新しいAsyncTask <、ボイドボイド、ブール>()ブールdoInBackground(最終ボイド...のparams){// 保護{ \t \t \t @Override \t \t \tでメソッドを呼び出します。 .. } ' – Cornelius

+0

あなたの答えはこの質問にあります:http://stackoverflow.com/questions/9365829/filenotfoundexception-for-httpurlconnection-in-ice-cream-sandwich – Kekoa

答えて

0
connection.setRequestMethod("POST"); 
connection.setDoOutput(false); 

だけ

connection.setDoOutput(true) 

connection.setRequestMethod("POST") 

前の文

0

私の.htaccessファイルを置くのhttpにWWWをリダイレクト書き換えルールを持っていた://。 Javaコードがうまくいきました。リダイレクト/リライトルールは、httpに転送する私の第1の要求(POST)を行い、したがってGETに「なった」。私のPHPスクリプトはPOSTだけを聞いていました。私は、自分の間違いがそのリダイレクトルールを持つまで私は頭を傷つけました。この種の「問題」を確認してください。

私にとっては、setDoOutputまたはsetRequestMethodのいずれかを設定することは重要ではありません。注文はすべて機能します(API 23)。現時点では、この順番になっています:

HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
con.setInstanceFollowRedirects(false); 
con.setUseCaches(false); 
con.setRequestMethod("POST"); 
con.setDoOutput(true); 
con.setRequestProperty("Content-Type",bla bla 
con.setRequestProperty("Accept-Charset", "UTF-8"); 
con.setRequestProperty("Content-Length", String.valueOf(PARAMETER_VARIABLE.getBytes().length)); 
con.setFixedLengthStreamingMode(PARAMETER_VARIABLE.getBytes().length); 
OutputStream os = con.getOutputStream(); 
os.write(PARAMETER_VARIABLE.getBytes()); 
os.flush(); 
os.close(); 
関連する問題