Java HTTPPost未发送正确的值
这是今天关于Android的第三个问题,这开始让人尴尬了。我正在尝试向此PHP页面发送HTTP POST请求,我正在尝试从MySQL数据库检索相册信息:Java HTTPPost未发送正确的值,java,php,android,http-post,Java,Php,Android,Http Post,这是今天关于Android的第三个问题,这开始让人尴尬了。我正在尝试向此PHP页面发送HTTP POST请求,我正在尝试从MySQL数据库检索相册信息: <?php if (isset($_POST['req']) && $_POST['req'] != '') { $request = $_POST['req']; // Including the handler for the database require_once 'include/d
<?php
if (isset($_POST['req']) && $_POST['req'] != '') {
$request = $_POST['req'];
// Including the handler for the database
require_once 'include/db_functions.php';
$db = new db_functions();
// Array for response
$response = array("req" => $request, "success" => 0, "error" => 0);
// Check the request type
// Obtains a list of medias for the listview.
if ($request == 'listemedias'){
$mediaList = $db->getMediasList();
// Parse the media list
for($i = 0; $i < count($mediaList); $i++){
$response["mediaList"][$i]["id"] = $mediaList[$i]['idmedia'];
$response["mediaList"][$i]["titre"] = $mediaList[$i]['titre'];
$response["mediaList"][$i]["annee"] = $mediaList[$i]['annee'];
$response["mediaList"][$i]["duree"] = $mediaList[$i]['duree'];
$response["mediaList"][$i]["jaquette"] = $mediaList[$i]['jaquette'];
}
$response["success"] = 1;
echo json_encode($response);
exit;
}
// Obtains specific informations for a specific media.
if ($request == 'media') {
if (isset($_POST['no']) && $_POST['no'] != '') {
$no = $_POST['no'];
$media = $db->getMedia($no);
if ($media != false){
$response["success"] = 1;
$response["media"]["id"] = $media["idmedia"];
$response["media"]["titre"] = $media["titre"];
$response["media"]["annee"] = $media["annee"];
$response["media"]["duree"] = $media["duree"];
$response["media"]["jaquette"] = $media["jaquette"];
$response["media"]["genre"] = $media["nomgenre"];
$response["media"]["editeur"] = $media["nomediteur"];
$response["media"]["collection"] = $media["nomcollection"];
} else {
echo 'Media does not exist';
exit;
}
echo json_encode($response);
exit;
} else {
echo 'Access Denied';
exit;
}
}
} else {
echo 'Access Denied';
exit;
}
?>
问题是,我只能从HTTP请求中得到“Access Deniedn”(是的,带有n)作为回复。以下是我的android java代码:
以下是为请求设置参数的函数:
public JSONObject getMediaList(){
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
// We add the request name to get all medias from the API service
params.add(new BasicNameValuePair("req","listemedias"));
// We get the JSON Object from the API service
JSONObject json = jsonParser.getJSONFromUrl(url, params);
return json;
}
公共JSONObject getMediaList(){
列表参数=新的ArrayList(1);
//我们添加请求名称以从API服务获取所有媒体
添加(新的BasicNameValuePair(“req”、“ListMedias”);
//我们从API服务获取JSON对象
JSONObject json=jsonParser.getJSONFromUrl(url,参数);
返回json;
}
下面是执行实际请求的JSONParser类的getJSONFromURL函数:
public JSONObject getJSONFromUrl (String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
publicJSONObject getJSONFromUrl(字符串url,列表参数){
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.附加(第+行“n”);
}
is.close();
json=sb.toString();
Log.e(“JSON”,JSON);
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
我很抱歉发布了这篇文章,我是Android新手,几天来我一直在努力想办法解决这个问题
编辑:经过多次调试后,参数确实包含“req”作为名称和与之关联的“ListMedias”。为什么它根本不起作用?这是一个编码问题吗?是否有任何方法可以验证为请求发送的标头
EDIT2:经过一些额外的修补,我就是想不出来,我要认输了。有没有其他方法可以让我使用呢?你可以发布日志吗?请在php文件的开头给echo$\u post。它将为您提供发布的参数。试试这个。