0
我是android新手。我想调用简单的PHP脚本,回显一些基本的字符串,并且我想在android应用程序中的textView中显示该字符串。但是当我点击我的按钮调用PHP脚本没有任何事情发生。这里是我的Android代码:android和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。 请问谁能告诉我哪里是错误。
我认为_you_需要告诉_us_错误在哪里,而不是相反。你的logcat会说什么吗?你做了什么来尝试调试这个(除了看到按钮不起作用并在这里发布)? –
尝试在您的移动浏览器中运行http://127.0.0.1/testic.php。如果没有出现,原因很简单。 Android设备无法看到本地服务器及其页面。 –