开发者

android http post response format type different than specified in query string

开发者 https://www.devze.com 2023-02-19 13:36 出处:网络
I am sending post request from android to the url using httpclient like this one below - String url = \"http://www.myurl.com/test.php?format=json\";

I am sending post request from android to the url using httpclient like this one below -

String url = "http://www.myurl.com/test.php?format=json";

Now I am getting response back in form of plist instead of json, which is default type of response from php file if no format query string is described in url.

Can we specify query string in url while posting? It looks like php file is not getting format query string.

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("method", "add"));
nameValuePa开发者_运维百科irs.add(new BasicNameValuePair("device_id", "123"));
nameValuePairs.add(new BasicNameValuePair("device_token", "3221"));
nameValuePairs.add(new BasicNameValuePair("session_id", "1212"));
nameValuePairs.add(new BasicNameValuePair("event_id","12345" ));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);                      httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
httppost.addHeader("Accept","application/json");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);

/* Checking response */
if (response != null) {
     InputStream in = response.getEntity().getContent();
     String a = convertStreamToString(in);
    Log.i("Read from Server "+response.getStatusLine().getStatusCode(), a);
}else{
    Log.i("response is ", "null");

}

Thanks


It's impossible to know without seeing your server code, but your server may not like that you're combining HTTP POST parameters in your request body with GET parameters in the URL. Try putting

nameValuePairs.add(new BasicNameValuePair("format","json" ));

into the section where you're building up the request parameters instead and see what happens.


Here is my server code

<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 'on' );

if (file_exists("lib")) {
require_once('lib/CFPropertyList-1.0.4/CFPropertyList.php');
include_once("lib/iCon_db_manager.php");
include_once("lib/iConLib.php");
} else {
require_once('../lib/CFPropertyList-1.0.4/CFPropertyList.php');
include_once("../lib/iCon_db_manager.php");
include_once("../lib/iConLib.php");
}

global $_POST;

$method = $_POST["method"];
$event_id = $_POST["event_id"];
if (isset($_POST["session_id"])) {
$session_id = $_POST["session_id"];
}
$device_id = $_POST["device_id"];
$device_token = $_POST["device_token"];

if (strcmp($method, "add") &&
strcmp($method, "delete") &&
strcmp($method, "show")) {
retStatus(1, "Invalid method. Allowed values are add/delete/show");
exit;
}

if (!isset($event_id) || !isset($device_id) || !isset($device_token)) {
retStatus(1, "Event ID, Device ID and Device Token are required");
exit;
}

$scheduleInfo["event_id"] = $event_id;
$scheduleInfo["device_id"] = $device_id;
$scheduleInfo["device_token"] = $device_token;

if (!isset($session_id)) {
// if session_id is not specified either the method is show alerts
$session_id = "";
}
if ($session_id != "") {
$scheduleInfo["session_id"] = $session_id;
}

$iConDBMgr = new iConDBManager();
if (!strcmp($method, "add")) {
if ($session_id == "") {
retStatus(1, "Session ID is required for add");
exit;
}
$schedule = $iConDBMgr->getRecord('iconmyschedule', $scheduleInfo);
if (count($schedule) == 0) {
$iConDBMgr->insertRecord('iconmyschedule', $scheduleInfo);
retStatus(0, "Success");
exit;
} else {
retStatus(2, "You already have this item in your schedule");
exit;
}
} else if (!strcmp($method, "delete")) {
$iConDBMgr->deleteRecord('iconmyschedule', $scheduleInfo);
retStatus(0, "Success");
exit;
} else {
$items = $iConDBMgr->getRecord('iconmyschedule', $scheduleInfo);
$plist = new CFPropertyList();
$plist->add( $dict = new CFDictionary() );
$dict->add( 'event_id', new CFString($event_id));
$dict->add( 'device_id', new CFString($device_id));
$dict->add( 'device_token', new CFString($device_token));
$dict->add('sessions', $sessArray = new CFArray());

if (count($items) != 0) {
foreach ($items as $item) {
$sessArray->add(new CFString($item{"session_id"}));
}
}
retResult($plist);
}

?>

0

精彩评论

暂无评论...
验证码 换一张
取 消