问题描述
我对android和PHP编程还很陌生,我现在在从php页面打印我的echo语句时遇到了问题,这很简单:
I'm fairly new to android and PHP programming and I am currently running into a problem printing my echo statement from my php page, which is as simple as:
<?php
echo "Hey there response!";
?>
我目前在MainActivity中使用的是:
What I am currently using in my MainActivity is:
setContentView(R.layout.activity_main);
TextView txtView = (TextView) findViewById(R.id.txt);
//ASYNC WAY
new GetData(txtView).execute("");
其中AsyncTask的定义是:
Where AsyncTask is defined by:
private class GetData extends AsyncTask<String, Void, String>{
private TextView display;
GetData(TextView view){
this.display = view;
display = (TextView) findViewById(R.id.txt);
}
protected String doInBackground(String... message){
HttpClient httpclient;
HttpGet request;
HttpResponse response = null;
String result = "error0";
try{
httpclient = new DefaultHttpClient();
request = new HttpGet("http://localhost/php/wamp.php");
response = httpclient.execute(request);
} catch (Exception e){
result = "error1";
}
try{
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line="";
while((line = rd.readLine()) != null){
result = result + line;
}
} catch(Exception e){
result = "error2";
}
return result;
}
protected void onPostExecute(String result){
this.display.setText(result);
}
}
txtView中的消息变为error2,我有不知道为什么。
The message in the txtView becomes error2, and I have no idea why.
编辑
我本来是使用非AsyncTask方法读取输入流的,但是我已经切换到AsyncTask由于网络错误。但是,由于我的应用程序中没有得到正确的回显,问题仍然存在。
EDITI was originally using a non-AsyncTask method of reading in input stream but I have switch to AsyncTask due to Networking error. The problem still persists though since I am not getting the corrent echo in my application.
推荐答案
对于yanki来说可能为时已晚,但
This is probably too late for yanki but for anybody else that comes across this page.
我用yanki的代码尝试了此操作,并得到了与他错误2相同的错误。那时,我注意到我没有更新清单文件以允许互联网权限。
I tried it with yanki's code and got the same error as him "error 2". I noticed then that I hadn't updated the manifest file to allow internet permissions.
<uses-permission android:name="android.permission.INTERNET" />
我再次运行它,效果很好。因此,使用yanki的代码并在清单文件中输入Internet权限,程序就可以运行。
I ran it again and it worked perfectly. So using yanki's code and entering the internet permissions in the manifest file the program should work.
这篇关于如何在我的Android应用程序上打印PHP echo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!