Android PHP Hello World

时间:2013-12-16 04:52:04

标签: php android

我是Android新手,但在PHP工作。我听说我们可以从Android调用PHP,因此我认为学习这个并期待将来有更多工作是个好主意。

基于Stack Overflow,我尝试将Hellow world从PHP写入Android

<?php
    echo 'Hello World';
?>

在Stack溢出中提到它需要包含以下代码。我指的是以下

How to retrieve a simple text like 'Hello world' from PHP to Android。它提到下面的代码需要在android manifest add中添加。但我没有得到它。

请让我知道这个代码包含在android中的位置。 (注意:我可以在android中显示hello world,但想从PHP中显示相同的内容)。如果您有添加,删除的任何示例,请从Android更新调用PHP的recrod,请分享我。

String myString;
String url = "http://localhost/Androidphp/ind.php";
HttpClient httpclient = new DefaultHttpClient();

HttpGet request;
try {
    request = new HttpGet(new URI(url));

    request.addHeader("User-Agent", "Android");


    HttpResponse response = httpclient.execute(request);

    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        out.close();
        myString = out.toString();
    }
} catch (URISyntaxException e1) {

    e1.printStackTrace();
} catch (ClientProtocolException e) {

    e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

3 个答案:

答案 0 :(得分:0)

您必须将以下权限添加到(<YOUR PROJET/res/AndroidManifest.xml>)文件_

<uses-permission android:name="android.permission.INTERNET"></uses-permission>`

刚刚经过this tutorial - How to connect Android with PHP, MySQL。您可以在此处获得演示应用程序代码,以帮助您更好地理解它。

我希望这会对你有所帮助!

答案 1 :(得分:0)

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;

public String convertResponseToString(HttpResponse response)抛出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;
   }

答案 2 :(得分:0)

我知道我对游戏迟到了,但是对于遇到过这篇文章的人来说,因为HTTPClient被贬低了,我仍然可以使用volley与php对话。我使用它来使用php来使自己能够从我的android应用程序连接到mySQL数据库。