2013-12-10 46 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正确执行我的手机浏览器 –