我是安卓新手。我想调用简单的 php 脚本来回显一些基本字符串,并且我想在 android 应用程序的 textView 中显示该字符串。但是当我单击调用 php 脚本的按钮时,什么也没有发生。这是我的安卓代码:
public void phpConnection(View v){
et = (EditText)findViewById(R.id.edit_message);
final ProgressDialog p = new ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server");
//TextView tv = (TextView)findViewById(R.id.tv);
//tv.setText("Response from PHP");
Thread thread = new Thread()
{
@Override
public void run() {
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://127.0.0.1/testic.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(1);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("Edittext_value",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
p.dismiss();
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Response from PHP" + response);
}
});
}catch(Exception e){
runOnUiThread(new Runnable() {
public void run() {
p.dismiss();
}
});
System.out.println("Exception : " + e.getMessage());
}
}
};
thread.start();
}
这是简单的 testic.php 脚本:
<?php
$val="My name is Nedim";
echo $val;
}
?>
此脚本保存在 wamp 服务器中。拜托,谁能告诉我哪里出错了。