我是 android 的新手,我正在尝试为用户单击按钮时执行简单的 php 脚本创建代码,并将字符串从 php 返回到 android 应用程序。这是我的代码:
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<NameValuePair> 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<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> 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();
}
}
}
这是我的php文件,它保存在wamp中,我启动了wamp,并为android应用添加了内部权限:
<?php
//code to reverse the string
$reversed = strrev($_POST["action"]);
echo $reversed;
?>
但是当我单击按钮时,我得到 IllegalStateException:Could not execute method of Activity 错误,但我不知道哪里出错了。请谁能告诉我我错过了什么。
这是我的 logcat 内容:
11-05 07:43:06.131: E/AndroidRuntime(833): 致命异常: main 11-05 07:43:06.131: E/AndroidRuntime(833): java.lang.IllegalStateException: 无法执行活动 11 的方法-05 07:43:06.131: E/AndroidRuntime(833): 在 android.view.View$1.onClick(View.java:3597) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 android. view.View.performClick(View.java:4202) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 android.view.View$PerformClick.run(View.java:17340) 11-05 07: 43:06.131: E/AndroidRuntime(833): 在 android.os.Handler.handleCallback(Handler.java:725) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 android.os.Handler.dispatchMessage (Handler.java:92) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 android.os.Looper.loop(Looper.java:137) 11-05 07:43:06.131: E/AndroidRuntime (833): 在 android.app.ActivityThread.main(ActivityThread.java:5039) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 java .lang.reflect.Method.invoke(Method.java:511) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 793) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-05 07:43:06.131: E/AndroidRuntime( 833):在 dalvik.system.NativeStart.main(本机方法)11-05 07:43:06.131:E/AndroidRuntime(833):引起:java.lang.reflect.InvocationTargetException 11-05 07:43:06.131: E/AndroidRuntime(833): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 java.lang.reflect.Method.invoke(Method. java:511) 11-05 07:43:06.131:E/AndroidRuntime(833): 在 android.view.View$1.onClick(View.java:3592) 11-05 07:43:06.131: E/AndroidRuntime(833): ... 11 更多 11-05 07:43 :06.131:E/AndroidRuntime(833):由:android.os.NetworkOnMainThreadException 11-05 07:43:06.131:E/AndroidRuntime(833):在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117 ) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 libcore .io.IoBridge.connectErrno(IoBridge.java:127) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 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): 在 java.net.Socket.connect(Socket.java:842) 11-05 07:43:06.131: E/AndroidRuntime(833): 在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): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 com.example.phptest.MainActivity。 posaljiphp(MainActivity.java:66) 11-05 07:43:06.131: E/AndroidRuntime(833): ... 14 更多E/AndroidRuntime(833): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 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 更多E/AndroidRuntime(833): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 11-05 07:43:06.131: E/AndroidRuntime(833): 在 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 更多