2016-12-06 22 views
0

このようなタイトルを持つ文字通り数多くのStack Overflowエントリがありますが、それらの問題のすべてが該当しないようで、サンプルソリューションは私のコードのようです。問題は次のような理由で、ポストを介してWebページにデータを送信しないように見えることです。Android HttpURLConnection投稿データを送信することができません

期待される動作:

{timestamp}/com.example.test D/Test: url: http://data.cubscout.local/enter_game_design 
{timestamp}/com.example.test D/Test: json sent 
{timestamp}/com.example.test D/Test: http response code: 200 
{timestamp}/com.example.test D/Test: {"test_value":"test"} 
{timestamp}/com.example.test D/Test: result read 
logcatで

{"test_value":"test"}ため、ページのエラー・ログに印刷する:活動の「テスト」ボタンを押すと、次の出力が発生します。

実際の動作:アクティビティに「テスト」ボタンを押すと、次の出力原因:logcatで

{timestamp}/com.example.test D/Test: url: http://data.cubscout.local/enter_game_design 
{timestamp}/com.example.test D/Test: json sent 
{timestamp}/com.example.test D/Test: http response code: 200 
{timestamp}/com.example.test D/Test: result read 

をし、ラインがちょうどタイムスタンプ

あるWebページのエラーログに出力されます

編集:curlのようなコマンドを使用してデータを送信すると、予期した動作が発生することに注意してください。例えば。 curl -i -H "Accept: application/json" -X POST -d {"test_value":"test"} http://da/enter_game_design/ {"test_value":"test"}はcurlで受信され、ログに記録されます。これは、サーバーの応答を受け取るコードに問題があっても、最初にデータを送信するコードが壊れていると結論づけます。

編集2:私がテストしていることLog.d("Test", IOUtils.toString(connection.getInputStream()));意志を正しく別のページ

MainActivity.javaへのURLを変更することで、ログに実際に印刷出力:

package com.example.test; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

import org.apache.commons.io.IOUtils; 

import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 

/** 
* Created by trevor on 12/5/16. 
*/ 

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button testButton = (Button)findViewById(R.id.button); 
     testButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new AsyncUploadTest().execute(); 
      } 
     }); 
    } 
    private class AsyncUploadTest extends AsyncTask<Void,Void,Boolean> 
    { 
     @Override 
     protected Boolean doInBackground(Void... v) { 
      try { 
       URL url = new URL("http://data.cubscout.local/enter_game_design"); 

       HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
       Log.d("Test","url: "+connection.getURL()); 

       connection.setUseCaches(false); 
       connection.setDoOutput(true); 
       connection.setDoInput(true); 

       connection.setRequestMethod("POST"); 
       connection.setRequestProperty("Connection", "Keep-Alive"); 
       connection.setRequestProperty("Cache-Control", "no-cache"); 
       connection.setRequestProperty("Content-Type","application/json;charset=utf-8"); 
       connection.setRequestProperty("Accept","application/json"); 

       OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); 
       writer.write("{\"test_value\":\"test\"}"); 
       writer.flush(); 
       writer.close(); 

       Log.d("Test","json sent"); 

       int httpResult = connection.getResponseCode(); 
       Log.d("Test","http response code: "+httpResult); 
       if(httpResult == HttpURLConnection.HTTP_OK) 
       { 
        Log.d("Test", IOUtils.toString(connection.getInputStream())); 
        Log.d("Test", "result read"); 
       } 

      } catch (MalformedURLException e) { 
       Log.e("Test","exception while sending data to server",e); 
      } catch (ProtocolException e) { 
       Log.e("Test","exception while sending data to server",e); 
      } catch (IOException e) { 
       Log.e("Test","exception while sending data to server",e); 
      } 
      return true; 
     } 
    } 
} 

activity_main.xml(レイアウトファイル)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:text="Test" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" /> 
</LinearLayout> 

AndroidManifest.xmlを

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.test"> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="Test"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

WebページのためのPHPスクリプトは

<?php 
    $inputString = file_get_contents("php://input"); 
    error_log($inputString); 
    echo $inputString; 
?> 
+0

FWIWと無関係:HttpURLConnectionの行動を考えると、私は、取り扱いには、以下を実行して、POSTリクエストのために自分自身をリダイレクト推薦するアプリケーション/ JSONにはcharsetパラメータはありません。 –

答えて

0

最初にHttpURLConnectionを接続すると、応答コードは301 moved permanentlyになります。これは、URLに終わりがないためです/HttpURLConnectionは、最初の接続で提供されたURLと透過的に接続を再作成しますが、ポストデータは再送信しません。この場合の問題の解決は簡単です。スラッシュを自分で追加してください。

try { 
    URL url = new URL("http://data.cubscout.local/enter_game_design"); 
    boolean redirect = false; 

    do { 
     redirect = false; 
     HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
     Log.d("Test","url: "+connection.getURL()); 

     connection.setUseCaches(false); 
     connection.setDoOutput(true); 
     connection.setDoInput(true); 

     connection.setInstanceFollowRedirects(false); 

     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("Cache-Control", "no-cache"); 
     connection.setRequestProperty("Content-Type","application/json;charset=utf-8"); 
     connection.setRequestProperty("Accept","application/json"); 

     OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); 
     writer.write("{\"test_value\":\"test\"}"); 
     writer.flush(); 
     writer.close(); 

     Log.d("Test","json sent"); 

     int httpResult = connection.getResponseCode(); 
     Log.d("Test","http response code: "+httpResult); 
     if(httpResult == HttpURLConnection.HTTP_OK){ 
      Log.d("Test", IOUtils.toString(connection.getInputStream())); 
      Log.d("Test", "result read"); 
     } 
     else if( httpResult == HttpURLConnection.HTTP_MOVED_PERM 
       || httpResult == HttpURLConnection.HTTP_MOVED_TEMP 
       || httpResult == HttpURLConnection.HTTP_SEE_OTHER){ 
      redirect = true; 
      url = new URL(connection.getHeaderField("Location")); 
     } 
    } while (redirect); 
} catch (MalformedURLException e) { 
    Log.e("Test","exception while sending data to server",e); 
} catch (ProtocolException e) { 
    Log.e("Test","exception while sending data to server",e); 
} catch (IOException e) { 
    Log.e("Test","exception while sending data to server",e); 
} 
0

をアクセスされている応答コードがHTTP_OKあるときは、応答を得ることができます。

あなたのコードの問題は、サーバーから実際のオブジェクトをフェッチしていないということです。実際のデータ/オブジェクトをフェッチするためには、レスポンスコードをフェッチするだけです。バッファリングされたリーダーを作成し、以下のように入力ストリームを生成する。

はこれを試してみてください。

int httpResult = connection.getResponseCode(); 
Log.d("Test","http response code: "+httpResult); 

if(httpResult == HttpURLConnection.HTTP_OK) { 
    Log.d("Test",IOUtils.toString(connection.getInputStream())); 
    Log.d("Test", "result read"); 

    //get response 
    BufferedReader in = new BufferedReader(
     new InputStreamReader(connection.getInputStream())); 
    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 

    in.close(); 

    //print result 
    System.out.println(response.toString()); 
} 

あなたはJSONに応答文字列を変換するためのJSONコンバーターを使用する必要があります。

+0

私は 'IOUtils.toString(connection.getInputStream())'があなたのコードに加えたことをすると考えていました。さらに、phpスクリプトは投稿情報をエラーログに出力し、上記のアンドロイドコードで空行を表示しますが、 'curl 'のようなコマンドラインツールを使用してデータを送信すると、データがログに出力されます。私は、データが正しく送信されていないことを合理的に確信しています。 –

関連する問題