title: Sending Data from Android App to Php
body: <p>I have a php form running in WAMP server on a local network ... But when I try to run that file it gives me the error in the screenshot. I am trying to send data from my Android app to Php form . Also my Android app crashes on pressing the "Send" Button</p>
  
  <p>I have made sure :</p>
  
  <ol>
  <li>Both WAMP and Android app are on the same network</li>
  <li>Also I tried to directly access the URL from android browser and it brings up the page , so URL should not be a trouble</li>
  <li>I have tried using a real device and its the same error .</li>
  <li>Also I have added INTERNET permission in Android Manifest!</li>
  </ol>
  
  <p><img src="https://i.stack.imgur.com/zROT7.png" alt="enter image description here"></p>
  
  <p>PHP code :</p>
  
  <pre><code>&lt;?php
  // get the "message" variable from the post request
  // this is the data coming from the Android app
  $message=$_POST["message"]; 
  // specify the file where we will save the contents of the variable message
  $filename="androidmessages.html";
  // write (append) the data to the file
  file_put_contents($filename,$message."&lt;br /&gt;",FILE_APPEND);
  // load the contents of the file to a variable
  $androidmessages=file_get_contents($filename);
  // display the contents of the variable (which has the contents of the file)
  echo $androidmessages;
  ?&gt;
  </code></pre>
  
  <p>And here is the Android App code :</p>
  
  <pre><code>public class HelloWorldActivity extends Activity {
  
      Button sendButton;
  
      EditText msgTextField;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          // load the layout
          setContentView(R.layout.main);        
  
          // make message text field object
          msgTextField = (EditText) findViewById(R.id.msgTextField);
          // make send button object
          sendButton = (Button) findViewById(R.id.sendButton);
  
      }
  
      // this is the function that gets called when you click the button
      public void send(View v)
      {
          // get the message from the message text box
          String msg = msgTextField.getText().toString();  
  
          // make sure the fields are not empty
          if (msg.length()&gt;0)
          {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost("http://192.168.0.40/yourPhpScript.php");
           try {
             List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
             nameValuePairs.add(new BasicNameValuePair("id", "12345"));
             nameValuePairs.add(new BasicNameValuePair("message", msg));
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             httpclient.execute(httppost);
             msgTextField.setText(""); // clear text box
           } catch (ClientProtocolException e) {
               // TODO Auto-generated catch block
           } catch (IOException e) {
               // TODO Auto-generated catch block
           }
  
          }
          else
          {
              // display message if text fields are empty
              Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
          }
  
      }
  }
  </code></pre>
  
  <p>Edit 1 : Here is the LogCat as requested for the original code</p>
  
  <pre><code>02-10 23:44:06.570: D/dalvikvm(1919): Not late-enabling CheckJNI (already on)
  02-10 23:44:06.720: D/dalvikvm(1919): GC_CONCURRENT freed 73K, 10% free 2785K/3092K, paused 13ms+0ms, total 16ms
  02-10 23:44:07.550: D/gralloc_goldfish(1919): Emulator without GPU emulation detected.
  02-10 23:44:17.428: D/AndroidRuntime(1919): Shutting down VM
  02-10 23:44:17.428: W/dalvikvm(1919): threadid=1: thread exiting with uncaught exception (group=0xb0f3e648)
  02-10 23:44:17.428: E/AndroidRuntime(1919): FATAL EXCEPTION: main
  02-10 23:44:17.428: E/AndroidRuntime(1919): java.lang.IllegalStateException: Could not execute method of the activity
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.view.View$1.onClick(View.java:3633)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.view.View.performClick(View.java:4240)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.view.View$PerformClick.run(View.java:17721)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.os.Handler.handleCallback(Handler.java:730)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.os.Handler.dispatchMessage(Handler.java:92)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.os.Looper.loop(Looper.java:137)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invokeNative(Native Method)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invoke(Method.java:525)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at dalvik.system.NativeStart.main(Native Method)
  02-10 23:44:17.428: E/AndroidRuntime(1919): Caused by: java.lang.reflect.InvocationTargetException
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invokeNative(Native Method)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invoke(Method.java:525)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.view.View$1.onClick(View.java:3628)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     ... 11 more
  02-10 23:44:17.428: E/AndroidRuntime(1919): Caused by: android.os.NetworkOnMainThreadException
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at libcore.io.IoBridge.connect(IoBridge.java:112)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at java.net.Socket.connect(Socket.java:842)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     at com.diwesh.helloworldactivity.HelloWorldActivity.send(HelloWorldActivity.java:73)
  02-10 23:44:17.428: E/AndroidRuntime(1919):     ... 14 more
  </code></pre>
  
  <p>Edit 2: LogCat after change from recommended code</p>
  
  <pre><code>02-11 19:28:35.991: D/AndroidRuntime(1945): Shutting down VM
  02-11 19:28:35.991: W/dalvikvm(1945): threadid=1: thread exiting with uncaught exception (group=0xb0f16648)
  02-11 19:28:35.991: E/AndroidRuntime(1945): FATAL EXCEPTION: main
  02-11 19:28:35.991: E/AndroidRuntime(1945): java.lang.IllegalStateException: Could not execute method of the activity
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.view.View$1.onClick(View.java:3633)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.view.View.performClick(View.java:4240)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.view.View$PerformClick.run(View.java:17721)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.os.Handler.handleCallback(Handler.java:730)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.os.Handler.dispatchMessage(Handler.java:92)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.os.Looper.loop(Looper.java:137)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.lang.reflect.Method.invokeNative(Native Method)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.lang.reflect.Method.invoke(Method.java:525)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at dalvik.system.NativeStart.main(Native Method)
  02-11 19:28:35.991: E/AndroidRuntime(1945): Caused by: java.lang.reflect.InvocationTargetException
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.lang.reflect.Method.invokeNative(Native Method)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.lang.reflect.Method.invoke(Method.java:525)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.view.View$1.onClick(View.java:3628)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     ... 11 more
  02-11 19:28:35.991: E/AndroidRuntime(1945): Caused by: android.os.NetworkOnMainThreadException
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at libcore.io.IoBridge.connect(IoBridge.java:112)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at java.net.Socket.connect(Socket.java:842)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     at com.diwesh.helloworldactivity.HelloWorldActivity.send(HelloWorldActivity.java:93)
  02-11 19:28:35.991: E/AndroidRuntime(1945):     ... 14 more
  </code></pre>