このようなタイトルを持つ文字通り数多くの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;
?>
FWIWと無関係:
HttpURLConnection
の行動を考えると、私は、取り扱いには、以下を実行して、POSTリクエストのために自分自身をリダイレクト推薦するアプリケーション/ JSONにはcharsetパラメータはありません。 –