I am making a POST request using retrofit in android to a server which i have created in Node.js. I have already worked on GET request and it is working fine but when i am making a post request i am not able to get the data.
My Code are as follows:
DataService.java
public class DataService extends Service{
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
try {
computeDistance(startLocation);
} catch (Exception e) {
e.printStackTrace();
}
stopSelf(msg.arg1);
}
public void computeDistance(String endLocation){
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.Http.URL_BASE)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
ApiService cityService = restAdapter.create(ApiService.class);
cityService.updateCityData();
}
}
}
I defined the interface like:
ApiService.java
public interface ApiService {
@POST("/city/update")
public String updateCityData();
}
My Node.js file is like:
function updateDBData(req, res, next) {
console.log('Entered into the UpdateDBDATa');
var sapi = {"data": "false"};
res.send(200, sapi).end();
}
/**-------- creating a server --------**/
var server = restify.createServer({name: 'crunchbase'});
server.get('/city/:name', updateDBData);
/**------- server listening port -------**/
server.listen(appContext.port, function () {
console.log('%s listening at %s', server.name, server.url);
});
When i am invoking the Node.js server with http://www.api.com:8084/search/update I am getting the message printed on the console. But when i am making retrofit request through android I am getting error like
D/Retrofit﹕ <--- HTTP 405 http://www.api.com:8084/city/update (439ms)<br>
D/Retrofit﹕ : HTTP/1.1 405 Method Not Allowed<br>
D/Retrofit﹕ Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5 , Content-Type, Date, X-Api-Version<br>
D/Retrofit﹕ Access-Control-Allow-Methods: GET<br>
D/Retrofit﹕ Access-Control-Allow-Origin: *<br>
D/Retrofit﹕ {"code":"BadMethod","message":"/city/update does not support POST"}
D/Retrofit﹕ <--- END HTTP (67-byte body)
I am not able to figure what is the error and how to resolve it.
Looks like the problem is on server side (you don't support post requests).
Try to support it by putting something like
server.post('/city/:name', updateDBData);
before
server.listen