我从android调用php url来接收一些字符串值 我的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";
?>
执行后我收到null给android。
答案 0 :(得分:1)
尝试打开您的网址:
http://10.10.2.26/demo
使用手机浏览器。可能你不在同一个网络中。
答案 1 :(得分: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) {
}
}