仮想マシンに保持されているバックエンド サーバーに HTTP データを送信する Android アプリを開発しています。
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("192.168.1.150/application/check.php");
params.add(new BasicNameValuePair("device", Build.SERIAL));
params.add(new BasicNameValuePair("person", "A"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
そして以下はphpコードです:
<?php
$file = "C:/wamp/www/application/file.txt";
$data = "device:".$_POST['device']." person:".$_POST['person'];
file_put_contents($file, $data);
?>
ただし、出力が表示されないため、ファイルを作成できないため、アプリがバックエンドサーバーに正常に接続できるかどうかを確認できません。モバイル デバイスとサーバー マシンの両方が同じ Wi-Fi ネットワーク内にあるため、仮想アドレスを入力することで、アプリがサーバーに接続できるはずですよね?
以下は、logcat のエラー メッセージです。
07-18 16:36:06.407: W/System.err(24598): android.os.NetworkOnMainThreadException
07-18 16:36:06.415: W/System.err(24598): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-18 16:36:06.415: W/System.err(24598): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-18 16:36:06.415: W/System.err(24598): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-18 16:36:06.415: W/System.err(24598): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-18 16:36:06.415: W/System.err(24598): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-18 16:36:06.415: W/System.err(24598): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-18 16:36:06.415: W/System.err(24598): at java.net.Socket.connect(Socket.java:842)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-18 16:36:06.423: W/System.err(24598): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-18 16:36:06.423: W/System.err(24598): at android.reader.Reader$1.onClick(Reader.java:106)
07-18 16:36:06.423: W/System.err(24598): at android.view.View.performClick(View.java:3511)
07-18 16:36:06.423: W/System.err(24598): at android.view.View$PerformClick.run(View.java:14105)
07-18 16:36:06.423: W/System.err(24598): at android.os.Handler.handleCallback(Handler.java:605)
07-18 16:36:06.423: W/System.err(24598): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 16:36:06.423: W/System.err(24598): at android.os.Looper.loop(Looper.java:137)
07-18 16:36:06.423: W/System.err(24598): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 16:36:06.423: W/System.err(24598): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:36:06.423: W/System.err(24598): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 16:36:06.423: W/System.err(24598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 16:36:06.423: W/System.err(24598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 16:36:06.423: W/System.err(24598): at dalvik.system.NativeStart.main(Native Method)
アプリを変更した後、logcat にメッセージが表示されます。
07-18 17:35:05.899: E/WindowManager(28043): Activity android.reader.Reader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416c3630 that was originally added here
07-18 17:35:05.899: E/WindowManager(28043): android.view.WindowLeaked: Activity android.reader.Reader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416c3630 that was originally added here
07-18 17:35:05.899: E/WindowManager(28043): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.Window$LocalWindowManager.addView(Window.java:537)
07-18 17:35:05.899: E/WindowManager(28043): at android.app.Dialog.show(Dialog.java:278)
07-18 17:35:05.899: E/WindowManager(28043): at android.reader.Reader$GrabURL.onPreExecute(Reader.java:219)
07-18 17:35:05.899: E/WindowManager(28043): at android.reader.Reader$1.onClick(Reader.java:101)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.View.performClick(View.java:3511)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.View$PerformClick.run(View.java:14105)
07-18 17:35:05.899: E/WindowManager(28043): at android.os.Handler.handleCallback(Handler.java:605)
07-18 17:35:05.899: E/WindowManager(28043): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 17:35:05.899: E/WindowManager(28043): at android.os.Looper.loop(Looper.java:137)
07-18 17:35:05.899: E/WindowManager(28043): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 17:35:05.899: E/WindowManager(28043): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 17:35:05.899: E/WindowManager(28043): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 17:35:05.899: E/WindowManager(28043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 17:35:05.899: E/WindowManager(28043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 17:35:05.899: E/WindowManager(28043): at dalvik.system.NativeStart.main(Native Method)
07-18 17:35:05.899: D/OpenGLRenderer(28043): Flushing caches (mode 1)