从android到PHP的HTTP post不工作
我正在尝试让安卓设备向本地主机发送一些信息。我相信我有Android发送信息,但我的PHP代码不接受或不显示代码。我已经附上我的代码,有什么我错过了?我也在运行wamp服务器,并已将权限放入清单中 Java代码:#从android到PHP的HTTP post不工作,php,android,Php,Android,我正在尝试让安卓设备向本地主机发送一些信息。我相信我有Android发送信息,但我的PHP代码不接受或不显示代码。我已经附上我的代码,有什么我错过了?我也在运行wamp服务器,并已将权限放入清单中 Java代码:# HttpPost-HttpPost; HttpClient-HttpClient; //列出参数及其值 列出nameValuePairs; 字符串响应酶; int服务器状态码; 字符串字节; 字符串serverURL=”http://10.0.2.2/test/index.php";
HttpPost-HttpPost;
HttpClient-HttpClient;
//列出参数及其值
列出nameValuePairs;
字符串响应酶;
int服务器状态码;
字符串字节;
字符串serverURL=”http://10.0.2.2/test/index.php";
httppost=新的httppost(服务器URL);
httpclient=新的DefaultHttpClient();
nameValuePairs=新的ArrayList(2);
//添加要发送到HTTP服务器的参数。
添加(新的BasicNameValuePair(“parameterName1”、“git”);
添加(新的BasicNameValuePair(“parameterName2”、“git”);
//将带有给定参数的POST消息发送到HTTP服务器。
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
InputStream is=response.getEntity().getContent();
BufferedInputStream bis=新的BufferedInputStream(is);
ByteArrayBuffer baf=新ByteArrayBuffer(20);
int电流=0;
而((当前=bis.read())!=-1){
baf.append((字节)当前值);
}
bytesSent=新字符串(baf.toByteArray());
//来自服务器的响应
serverResponsePhrase=response.getStatusLine().getReasonPhrase();
serverStatusCode=response.getStatusLine().getStatusCode();
系统输出打印项次(“完成”);
}捕获(例外e){
//异常处理
System.out.println(“问题是”+e.toString());
}
PHP代码:
<?php
echo "param1 value: ".$_POST['parameterName1']."\n";
echo "param2 value: ".$_POST['parameterName2']."\n";
?>
我也尝试过这段代码,但它在我的PHP中不起作用
HttpPost httppost;
HttpClient httpclient;
// List with arameters and their values
List<NameValuePair> nameValuePairs;
String serverResponsePhrase;
int serverStatusCode;
String bytesSent;
String serverURL = "http://10.0.2.2/test/index.php";
httppost = new HttpPost(serverURL);
httpclient = new DefaultHttpClient();
nameValuePairs = new ArrayList<NameValuePair>(2);
// Adding parameters to send to the HTTP server.
nameValuePairs.add(new BasicNameValuePair("'parameterName1'", "git"));
nameValuePairs.add(new BasicNameValuePair("'parameterName2'", "git"));
// Send POST message with given parameters to the HTTP server.
try {
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.addHeader(entity.getContentType());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
bytesSent = new String(baf.toByteArray());
// Response from the server
serverResponsePhrase = response.getStatusLine().getReasonPhrase();
serverStatusCode = response.getStatusLine().getStatusCode();
System.out.println("response" + response.toString());
System.out.println("COMPLETE");
} catch (Exception e) {
// Exception handling
System.out.println("Problem is " + e.toString());
}
HttpPost-HttpPost;
HttpClient-HttpClient;
//列出参数及其值
列出nameValuePairs;
字符串响应酶;
int服务器状态码;
字符串字节;
字符串serverURL=”http://10.0.2.2/test/index.php";
httppost=新的httppost(服务器URL);
httpclient=新的DefaultHttpClient();
nameValuePairs=新的ArrayList(2);
//添加要发送到HTTP服务器的参数。
添加(新的BasicNameValuePair('parameterName1',“git”);
添加(新的BasicNameValuePair('parameterName2',“git”);
//将带有给定参数的POST消息发送到HTTP服务器。
试一试{
HttpEntity实体=新的UrlEncodedFormEntity(nameValuePairs);
httppost.addHeader(entity.getContentType());
httppost.setEntity(实体);
HttpResponse response=httpclient.execute(httppost);
InputStream is=response.getEntity().getContent();
BufferedInputStream bis=新的BufferedInputStream(is);
ByteArrayBuffer baf=新ByteArrayBuffer(20);
int电流=0;
而((当前=bis.read())!=-1){
baf.append((字节)当前值);
}
bytesSent=新字符串(baf.toByteArray());
//来自服务器的响应
serverResponsePhrase=response.getStatusLine().getReasonPhrase();
serverStatusCode=response.getStatusLine().getStatusCode();
System.out.println(“response”+response.toString());
系统输出打印项次(“完成”);
}捕获(例外e){
//异常处理
System.out.println(“问题是”+e.toString());
}
确保设置了内容类型
HTTP头。试着替换
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
用这个
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.addHeader(entity.getContentType());
httppost.setEntity(entity);
另外,如果您想查看响应的主体,而不是
response.toString()
,请尝试EntityUtils.toString(response.getEntity())
,以确认Android代码是否正常工作:只需查看服务器的访问日志,查看是否有POST请求。如果你不明白这一点,那么Android方面的事情就根本不正常了。我正在运行一个wamp本地服务器。这听起来很愚蠢,但我该怎么检查呢?不知道wamp。检查apache的httpd.conf并查找“CustomLog”选项。127.0.0.1---[21/Sep/2011:16:37:12+0100]“POST/test/index.php HTTP/1.1”200 221我发现了,所以我假设Android正在工作,那么为什么我的php不工作呢?既然命中了apache,您的代码非常简单,我看不出它会失败(即使android没有发布任何数据,脚本仍然会输出一些文本),我建议您查看android中的响应处理程序。响应.whatever()调用。它似乎是从Android发送的,我认为这是PHP没有收到它的问题,我是在实现代码127.0.0.1--[21/Sep/2011:16:38:16+0100]“GET/test/HTTP/1.1”200 2090 127.0.0.1--[21/Sep/2011:16:38:47+0100]“GET/test/HTTP/1.1”200 2090 127.0.1--[21/Sep/2011:16:38:47+0100]“GET/test/HTTP/1”200 2090 127.0.1--[21/Sep/2011:16:40:40+0100]“POST/test/index.php HTTP/1.1”200 221能否告诉我们您从EntityUtils.toString(response.getEntity())
而不是response.toString()
?09-21 16:25:28.382:INFO/System.out(1032):问题是java.lang.IllegalStateException:内容已被消费显然这就是问题所在!但这意味着什么?我设法解决了这个问题,使用response.getEntity().getContent()时出现了双重问题;但是它仍然没有显示在我的wamp服务器上的PHP页面上。但是在我的系统中,out.print似乎在android上工作(在日志猫中),只是没有显示在Firefox中的index.PHP上。您在Firefox中采取了哪些步骤来访问index.PHP
?
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.addHeader(entity.getContentType());
httppost.setEntity(entity);