2013-12-10 134 views
0

我從android調用php url來接收一些字符串值。
我的Android代碼如下:php返回null http響應android

 private static final String BASE_URL="http://10.10.2.26/demo"; 
public static HttpResponse executeUrl(String action,String[] parameters) { 


    try { 
     HttpClient client = new DefaultHttpClient(); 



     URI website = new URI(BASE_URL+"/"+action); 


     System.out.println("serverurl="+website); 
     HttpGet request = new HttpGet(); 


     request.setURI(website); 

     System.out.println("5"); 
     HttpResponse response = client.execute(request); 


     System.out.println("no error in execute URL"); 
     return response; 
    } catch (Exception e) {   

     System.out.println("In error of execute URL"+e.getLocalizedMessage()); 
    } 
    return null; 
} 

以下是我的PHP代碼:

<?php 

echo "kunal"; 
?> 

執行後,我收到空到Android。

+0

請發佈您的PHP代碼 – Manitoba

+0

我添加了代碼,但沒有顯示。我的代碼是<?php echo「kunal」; ?> –

+0

Btw你的IP是本地地址。 – Manitoba

回答

1

從php中檢索值的代碼。它工作正常。

private void get_valueFromPhp(String php) { 
    String responseString = null; 

    try{  
     HttpClient httpclient = new DefaultHttpClient(); 
     String url ="your url"; 
     HttpPost httppost = new HttpPost(url); 
     HttpResponse response = httpclient.execute(httppost); 

     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     response.getEntity().writeTo(out); 
     out.close(); 
     responseString = out.toString(); 
     Toast.makeText(getApplicationContext(),responseString,1000).show(); 
    } catch(Exception e) { 
    } 
} 
+0

感謝這工作 –

+0

@Kunal Kor:歡迎您。快樂的編碼。 – Shahina

1

嘗試打開您的網址:

http://10.10.2.26/demo 

使用手機瀏覽器。可能你不在同一個網絡中。

+0

我目前正在仿真器上的筆記本電腦上運行。和url正確執行我的手機瀏覽器 –