Android PHP Hello World
我是Android新手,但在PHP工作。我听说我们可以从Android中调用PHP,因此我认为学习这一点是个好主意,并期待将来有更多的工作 基于堆栈溢出,我尝试将Hellow world从PHP编写到AndroidAndroid PHP Hello World,php,android,Php,Android,我是Android新手,但在PHP工作。我听说我们可以从Android中调用PHP,因此我认为学习这一点是个好主意,并期待将来有更多的工作 基于堆栈溢出,我尝试将Hellow world从PHP编写到Android <?php echo 'Hello World'; ?> 您必须在文件中添加以下权限_ 刚刚通过。这里有一个演示应用程序代码,可以帮助您更好地理解它 我希望这对你有帮助 try{ HttpClient httpcli
<?php
echo 'Hello World';
?>
您必须在文件中添加以下权限_ 刚刚通过。这里有一个演示应用程序代码,可以帮助您更好地理解它 我希望这对你有帮助
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/Androidphp/ind.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "+e.toString());
}
return null;
}
用那种方法
声明私有变量
InputStream inputStream;
公共字符串convertResponseToStringHttpResponse响应引发IllegalStateException,IOException{
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
System.out.println("content length "+contentLength);
// Toast.makeText(getApplicationContext(), "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..
}
return res;
}
我知道我在游戏中迟到了,但是对于那些因为HTTPClient被抹黑而看到这篇文章的人来说,我仍然能够使用php与php进行对话。我使用php使自己能够从android应用程序连接到mySQL数据库。你有没有尝试过简单的谷歌搜索,以获得使用android和php的教程?你在AndroidMenifest.xml中添加了权限了吗?很抱歉打断你,但是Android和PHP没有任何连接。你可以从Android设备上点击用PHP编写的网页,但它不符合Android的具体要求。我尝试了谷歌搜索和所有搜索,但没有任何效果。上面提到的以下代码需要在Android man中添加如果你没有在清单中写代码,对吗??
InputStream inputStream;
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
System.out.println("content length "+contentLength);
// Toast.makeText(getApplicationContext(), "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..
}
return res;
}