Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to call php script from android application
    primarykey
    data
    text
    <p>i am new in android, and am am trying to create code for when user click on button simple php script is executed, and string is returned to android app from php. Here is my code:</p> <pre><code>package com.example.phptest; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { HttpPost httppost; StringBuffer buffer; HttpResponse response; HttpClient httpclient; List&lt;NameValuePair&gt; nameValuePairs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void posaljiphp(View v){ try { // Create a new HttpClient and Post Header httpclient = new DefaultHttpClient(); httppost = new HttpPost("http://127.0.0.1/onako.php"); String MyName = "nedim"; //any data to send //This is the data to send // Add your data nameValuePairs = new ArrayList&lt;NameValuePair&gt;(1); nameValuePairs.add(new BasicNameValuePair("action", MyName)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler(); String response = httpclient.execute(httppost, responseHandler); //This is the response from a php application String reverseString = response; Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show(); } catch (ClientProtocolException e) { Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show(); } catch(IllegalStateException ex){ Toast.makeText(this,"Greska: " + ex.toString(), Toast.LENGTH_LONG).show(); } } </code></pre> <p>}</p> <p>and here is my php file, it's saved in wamp, and i started wamp, and add interner permission to android app:</p> <pre><code>&lt;?php //code to reverse the string $reversed = strrev($_POST["action"]); echo $reversed; </code></pre> <p>?></p> <p>but when i click on button i get IllegalStateException:Could not execute method of Activity error, but i don't know where am getting wrong. Please can anyone tell me what i am missing.</p> <p>here is my logcat content:</p> <p>11-05 07:43:06.131: E/AndroidRuntime(833): FATAL EXCEPTION: main 11-05 07:43:06.131: E/AndroidRuntime(833): java.lang.IllegalStateException: Could not execute method of the activity 11-05 07:43:06.131: E/AndroidRuntime(833): at android.view.View$1.onClick(View.java:3597) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.view.View.performClick(View.java:4202) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.view.View$PerformClick.run(View.java:17340) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.os.Handler.handleCallback(Handler.java:725) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.os.Handler.dispatchMessage(Handler.java:92) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.os.Looper.loop(Looper.java:137) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.app.ActivityThread.main(ActivityThread.java:5039) 11-05 07:43:06.131: E/AndroidRuntime(833): at java.lang.reflect.Method.invokeNative(Native Method) 11-05 07:43:06.131: E/AndroidRuntime(833): at java.lang.reflect.Method.invoke(Method.java:511) 11-05 07:43:06.131: E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 11-05 07:43:06.131: E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-05 07:43:06.131: E/AndroidRuntime(833): at dalvik.system.NativeStart.main(Native Method) 11-05 07:43:06.131: E/AndroidRuntime(833): Caused by: java.lang.reflect.InvocationTargetException 11-05 07:43:06.131: E/AndroidRuntime(833): at java.lang.reflect.Method.invokeNative(Native Method) 11-05 07:43:06.131: E/AndroidRuntime(833): at java.lang.reflect.Method.invoke(Method.java:511) 11-05 07:43:06.131: E/AndroidRuntime(833): at android.view.View$1.onClick(View.java:3592) 11-05 07:43:06.131: E/AndroidRuntime(833): ... 11 more 11-05 07:43:06.131: E/AndroidRuntime(833): Caused by: android.os.NetworkOnMainThreadException 11-05 07:43:06.131: E/AndroidRuntime(833): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 11-05 07:43:06.131: E/AndroidRuntime(833): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 11-05 07:43:06.131: E/AndroidRuntime(833): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 11-05 07:43:06.131: E/AndroidRuntime(833): at libcore.io.IoBridge.connect(IoBridge.java:112) 11-05 07:43:06.131: E/AndroidRuntime(833): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 11-05 07:43:06.131: E/AndroidRuntime(833): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 11-05 07:43:06.131: E/AndroidRuntime(833): at java.net.Socket.connect(Socket.java:842) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 11-05 07:43:06.131: E/AndroidRuntime(833): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 11-05 07:43:06.131: E/AndroidRuntime(833): at com.example.phptest.MainActivity.posaljiphp(MainActivity.java:66) 11-05 07:43:06.131: E/AndroidRuntime(833): ... 14 more</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload