2013-12-16 171 views
-2

我是Android新手,但在PHP中工作。我聽說我們可以從Android調用PHP,因此我認爲這是一個很好的主意,可以學習這一點,並期待未來有更多的工作。Android PHP Hello World

基於堆棧溢出,我試圖從PHP寫Hellow世界到Android

<?php 
    echo 'Hello World'; 
?> 

及其在堆棧溢出需要包括下面的代碼中提到。我指的是以下

How to retrieve a simple text like 'Hello world' from PHP to Android。它提到了下面的代碼需要添加到Android清單添加。但我不明白。

請讓我知道這個代碼被包含在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(); 
} 
+0

你試過一個簡單的谷歌搜索教程使用Android與PHP? http://bit.ly/1b9nbKs –

+0

您是否在'AndroidMenifest.xml'中添加了<使用權限android:name =「android.permission.INTERNET」/>'權限? – zanky

+0

是的,我已經添加 – user3080572

回答

0

你必須在下面添加允許您(<YOUR PROJET/res/AndroidManifest.xml>) _文件

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

剛剛經歷了this tutorial - How to connect Android with PHP, MySQL。你會在這裏獲得一個演示應用程序代碼,這有助於你更好地理解它。

我希望這會幫助你!

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; 

公共字符串convertResponseToString(HttpResponse對象響應)拋出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; 
    }