下面是一段试图发布到本地服务器上的PHP脚本的代码片段。我正在将数据传回服务器的MySQL表,因为数据量非常小,我想我会使用HttpClient将数据嵌入到URL参数中。
然而,即使我从服务器获得的响应是正常的(HTTP代码= 200),很明显服务器端PHP脚本没有获得正确格式化的数据(对MySQL表没有影响) 。但是,当我在浏览器中手动输入URL + args时:
http://10.0.0.13/jobs_returndata_test.php?jobnum=189193&pnum=3&entime=13:00&extime=14:00
例如,一切正常(PHP脚本正确地写入MySQL表)。
我的问题:我有没有办法真正查看HttpClient发送的内容? (即HttpPost对象的整个.toString内容?)。这是Android端代码段:
// Create a new HttpClient and Post Header, send data as args to PHP file
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.13/jobs_returndata.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("jobnum",convertSimple(jobNum)));
nameValuePairs.add(new BasicNameValuePair("pnum",convertSimple(rowNum)));
nameValuePairs.add(new BasicNameValuePair("entime",enteredInTime));
nameValuePairs.add(new BasicNameValuePair("extime",enteredOutTime));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.i("HTTP Post", "Execute Post sending jobnum="+ convertSimple(jobNum) +
"&pnum="+ convertSimple(rowNum) + "&entime=" + enteredInTime
+ "&extime=" + enteredOutTime);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.i("HTTP Post", "Response from server = " + response.getStatusLine().getReasonPhrase() + " Code = " + convertSimple(response.getStatusLine().getStatusCode()));
} catch (ClientProtocolException e) {
Log.e("HTTP Post", "Protocol error = " + e.toString());
} catch (IOException e) {
Log.e("HTTP Post", "IO error = " + e.toString());
}
我的服务器端PHP文件是:
?php
$jobnum=$_GET['jobnum'];
//echo "jobnum = $jobnum <br />";
$pnum=$_GET['pnum'];
//echo "pnum = $pnum <br />";
$entime=$_GET['entime'];
//echo "entime = $entime <br />";
$extime=$_GET['extime'];
//echo "extime = $extime <br />";
$dbh=mysql_connect ("localhost", "Steve", "pluggin");
mysql_select_db ("DriverJobs");
$result = mysql_query("UPDATE jobs SET EntryTime = '$entime' WHERE JobNumber = '$jobnum' AND PointNum = '$pnum' ");
$result = mysql_query("UPDATE jobs SET ExitTime = '$extime' WHERE JobNumber = '$jobnum' AND PointNum = '$pnum' ") ;
//echo "done <br />";
?>
相关部分的logcat:
06-22 13:33:11.341:I / HTTP Post(27788):执行发送后 jobnum = 189193&amp; pnum = 3&amp; entime = 9:00&amp; extime = 9:05 06-22 13:33:11.581: I / HTTP Post(27788):来自服务器的响应= OK Code = 200
权限很好(在INTERNET的Manifest文件中,对于PHP文件,Apache2正在运行,API = 8,使用带有Android模拟器的Eclipse)。 convertSimple()是一个2线程,它返回int原语的String等效项。
提前感谢您查看此内容!
答案 0 :(得分:6)
使用此方法
private String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
return sb.toString();
}
并像这样调用以查看post方法中包含的实体
try {
HttpPost postMethod = new HttpPost("your url");
Log.i(TAG,"HTTP Entiry : " + convertStreamToString(postMethod.getEntity().getContent()));
}catch (Exception e){
e.printStackTrace();
}
答案 1 :(得分:3)
在浏览器中输入变量时看到变量的原因是GET和POST之间的区别。当您使用浏览器时,您将以GET格式发送变量。
在你的php代码中使用:
$_POST['varname'];
或者您可以使用请求获取POST和GET vars:
$_REQUEST['varname'];
答案 2 :(得分:1)
我会使用fiddler来查看请求
答案 3 :(得分:-1)
HttpResponse 后添加代码如下所示
InputStream is;
try{
if( response == null ){
}else{
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("res",result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
其中结果将在字符串中给出我的响应我已在日志中打印以进行测试