I am using Retrofit in my android app to communicate with my server. In one of my server calls I am expecting a String response from server. So, I declare a callback which expects a string value. Callback<String>. In the php, I echo a string. Say echo "test"; When I hit the url in browser, the echo works as expected test. But in my android app, failure callback is called.
I tried changing the php to echo "\"test\"";
On browser : "test"
On android : success callback is called.
I solved it by declaring a variable.
Php:
$result = "test";
echo $result;
Browser: test
Android : success callback is called.
My question is, is this how Retrofit works? Or am I doing anything wrong? Also, to solve this is there any way other than declaring a variable?
Callback<String> does not make a lot of sense in the context of retrofit. By default retrofit operates using GSON.
What you are actually waiting for from the server is json deserialized into a POJO (simple java object).
Lets say you have a data model (POJO) like:
public class User {
public final String name;
}
Then you'd use a callback like this Callback<User>. And from the server you should do: echo '{ "name" : "Simon" }';
In your success callback you will have an instance of User class with the name field set to 'Simon'.
More on this here: http://square.github.io/retrofit/
Related
I have spent the past few days integrating Slim API to handle some PHP web services. The first few services utilized GET which was straight forward and had no problems. However, when trying to integrate some POST methods, I am receiving no response from the service. I have tried even just a simple echo to see if the service is being called. In every case, there is no return. Code below, some of the methods have been removed for clarity.
Any reason the POST method is unresponsive? Thanks! viv
$app->get('/login/:un/:pw/:type','login');
$app->get('/browseMO/:prm1/:prm2', 'browseMedia');
$app->get('/usersReviews/:userID','usersReviews');
$app->get('/pubsReviews/:userID','pubsReviews');
$app->get('/productReviews/:productID','getProductReviews');
$app->get('/productAvg/:productID','averageReviewsByOProduct');
$app->post('/userUpd','updateUserInfo');
$app->run();
function averageReviewsByOProduct($productID){
reviews::getAvgReviewByProduct($productID);
}
function browseMedia($param1, $param2){
browseMediaObjects::getMedia($param1, $param2);
}
function updateUserInfo(){
// $request = Slim::getInstance()->request();
// $body = $request->getBody();
echo "UPDATE CALLED"; // never reached
}
Try creating an anonymous function in the slim post declaration and see if the function gets call. If it doesn't, it's something with slim. If it does, it's something with your code.
$app->post('/userUpd',function() use ($app) {
echo 'Test';
});
If that doesn't work, make sure you are returning your data correctly. For instance the above returns data correctly for an AJAX call that expects a text string response.
In my CakePHP app I return JSON and exit for certain requests. An example of this would be trying to access the API for a login as a GET request:
header('Content-Type: application/json');
echo json_encode(array('message'=>'GET request not allowed!'));
exit;
However I am having to prefix the echo with the content type in order for it to be sent as JSON. Otherwise my code at the other end interprets it different.
Any ideas on how to get around this? Or at least improve it.
Update: Cake version 2.3.0
You can leverage the new 2.x response object:
public function youraction() {
// no view to render
$this->autoRender = false;
$this->response->type('json');
$json = json_encode(array('message'=>'GET request not allowed!'));
$this->response->body($json);
}
See http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse
Also you could use the powerful rest features and RequestHandlerComponent to achieve this automatically as documented: http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
You just need to allow the extension json and call your action as /controller/action.json.
Then cake will automatically use the JsonView and you can just pass your array in. It will be made to JSON and a valid response by the view class.
Both ways are cleaner than your "exit" solution - try to unit-test code that contains die()/exit(). This will end miserably. So better never use it in your code in the first place.
I have a WCF Service, the interfaces work fine when connecting with a c# application but when I connect using a PHP application all variables passed to the service are null.
This is the PHP code used to connect to the service and send the data.
$SelectedFolder = $_REQUEST['folder'];
var_dump($SelectedFolder);
try
{
$client = new SoapClient('http://localhost:8663/Service.svc?wsdl');
$Files = $client->GetAllLatestVersionsString($SelectedFolder);
}
The var dump displays the following
string 'Pictures/Sample/' (length=16)
This is the service code
[OperationContract]
List<VersionedFileDataModel> GetAllLatestVersionsString(string partUri);
I've tried passing a static value instead of a variable and both times the value received by the service is null.
Thanks in Advance for any help,
Matt
Fixed it, I was required to pass the variables through using an array of params, for the example I posted I had to change the code to this.
try
{
$client = new SoapClient('http://localhost:8663/Service.svc?wsdl');
$params->partUri = $SelectedFolder;
$Files = $client->GetAllLatestVersionsString($params);
}
It took me a while to get soap configured in php. Now I'm just trying to learn about it. I'm using the web service here to learn:
http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=19
It says the WSDL is here:
http://www.webservicex.net/stockquote.asmx?wsdl
and this is my code:
$client = new SoapClient('http://www.webservicex.net/stockquote.asmx?wsdl');
$result = $client->getQuote("AAPL");
echo $result;
I'm getting an error saying "Object of class stdClass could not be converted to string". Now I realize that $result is an object, and I'm wondering how do I access data that the server populated? I tried to understand the WSDL but I'm getting nowhere. Is it supposed to be something like:
$result->price
? (That doesn't work by the way...)
Ideas?
When curious about datatypes from the soapserver, use the SoapClient->__getTypes() & SoapClient->__getFunctions() methods
If you return is still not clear, var_dump() your return and examine it.
I'm currently working on a small application that works like this:
When the user clicks a link, an Ajax GET request is fired.
The request hits a server-side PHP script.
The script, which requests information for another domain, retrieves a JSON feed.
The feed is then echoed back to the client for parsing.
I'm not really a PHP developer, so I am looking for some best practices with respect to cross-domain requests. I'm currently using file_get_contents() to retrieve the JSON feed and, although it's functional, it seems like a weak solution.
Does the PHP script do anything other than simply call the other server? Do you have control over what the other server returns? If the answers are No and Yes, you could look into JSONP.
You might want to abstract the retrieval process in PHP with an interface so you can swap out implementations if you need too. Here is a naive example:
interface CrossSiteLoader
{
public function loadURL($url);
}
class SimpleLoader implements CrossSiteLoader
{
public function loadURL($url)
{
return file_get_contents($url);
}
}
Comes in handy if you need to test locally with your own data because you can use a test implementation:
public ArrayLoader implements CrossSiteLoader
{
public function loadURL($url)
{
return json_encode(array('var1' => 'value1', 'var2' => 'value2'));
}
}
or if you just want to change from file_get_contents to something like curl