How to send Json PUT request from Android to rails server - android

The following is the ajax code used by the website to change the two feilds ability_bpm and endurance_bpm
function editBatch_Ajax(id, batch_data){
$.ajax({
url: '/batches/'+ id,
type: 'put',
context: 'this',
dataType: 'json',
data: {
batch: batch_data,
id: id
},
success: function(msg){
$('#' + id + ' .' + Object.keys(batch_data)[0]).text(batch_data[Object.keys(batch_data)[0]]);
},
error: function(jqXHR, textStatus, errorThrown) {}
});
}
sends a PUT request from a Ajax call. Now I want to do the same thing from an android application. here
data: {
batch: batch_data,
id: id
},
will be something like
data : {
batch: {
ability_bpm: 40
},
id: 0
}
The website that I webserver is running at http://www.rudimentor.me/
I am a beginer to android. So far I have gotten to display the data available at the server in a listview I need to have this to change the values on the server. I have also tried to send request from a rest client in a browser but cannot figure it out

You can use this library to make your HTTP Calls:
http://loopj.com/android-async-http/
Here you can find some example how to use this library:
https://github.com/franzejr/android-boilerplate/blob/master/app/src/main/java/controllers/SampleController.java
And also you can use the GSON to help you handling JSONs. It's pretty straightforward.

I would say there are a number of options. I prefer Retrofit.
Here is a link to a question with some good comparisons of the various options.
Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

Related

Cordova doesn't add data to URL (AJAX)

I'm sending an Jquery.Ajax request to a server which is hosting a .asmx web service.
jQuery.ajax({
type: 'GET',
url: 'http://IP-destination/myFolder/WebService.asmx/LogonUser',
cache: 'true',
data: {
'username':get_cookie('username'),
'password':get_cookie('password'),
'environment':get_cookie('environment')
},
dataType: 'jsonp',
beforeSend: function(){$.mobile.loading('show', 'a', 'Laddar...', false); alert("go");},
error: function(a, b, c){
$.mobile.loading('hide','a');
alert(a + b+ c);
},
success: function(response){
$.mobile.loading('hide','a');
alert("success");
self.loginUser_cb(response);
}
});
While sending data through my browser, emulator or "Legacy Hybrid build (Intel XDK) for android" everything works great.
But when I use Cordova (still android, works on IOS) the request get an "500 Internal Server Error".
It seems like my URL is missing my data parameters:
http://oi60.tinypic.com/21jvb5y.jpg
When I use my browser the URL is correct with the data parameter in the end of the url: /LogonUser?callback=jQuery19105695250731240461_1425028837473&username=MyUserName&password=Secret&environment=Dev
My question is: What could possibly cause this in Cordova Android but work great
in the emulator/ios/Hybrid Legacy build? The URL seem to be cut short.
I'd look to simplify things as much as possible and see if the get_cookie portion is failing for you. Try removing that and using static values. If it works, it means your cookies may not exist as you think they should and you should write logic to handle that.

Android WebView AJAX Request

I'am develoing a VB.NET MVC4 Web App using Android's webView to run it.
I have the next problem: When i make an ajax PUT request to my own controllers method, i recieve a 404 status code.
In the other hand, when i make a get or post petition, works fine. ¿Any idea?
$.ajax({
type: 'PUT',
url: 'myurl',
data: data,
success: function (json, textStatus) {
alert(json);
},
error: function (jqXHR) {
alert('error');
}
});
jqXHR.readystate = 4
jqXHR.message = server error file or directory not found
jqXHR,status: 404
EDIT: IIS is configured to work with PUT and DELETE and thanks to that it works correctly in the browser but not with the Androids WebView.
Your code is missing a quote url: 'myurl needs a ' on the end
$.ajax({
type: 'PUT',
url: 'myurl',
data: data,
success: function (json, textStatus) {
alert(json);
},
error: function (jqXHR) {
alert('error');
}
});
This might be an error in your example but that's all we can see to correct the other things to check is IIS Setup of the site does is it set to allow PUT, DELETE ect
GET & POST are standard and are enabled by default.
So please follow the answer to this question:
How do I enable HTTP PUT and DELETE for ASP.NET MVC in IIS?

How to make ajax-like call in Android

I am working on Android application that will make consistent api calls to a server and parse JSON from the server. ( Actually this application is an web game.) Now we have an web version written with jQuery in which $.ajax() calls are used:
$.ajax({
url: "https://www.example.com/api/base_info/",
type: "POST",
dataType: "json",
crossDomain: true,
data: {
"client_type": "web",
"id": userID,
"access_key": accessToken
},
success: function (data) {
username = data.name;
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
}
});
So how can I duplicate this in Android? I have searched on the internet and I see many different approaches. I am not sure which is best for Android platform? Would you please give some suggestions? Please use the code above as an example if possible. Thanks in advance!

Gingerbread device throwing 411 on ajax requests

I am creating a phonegap app that needs to support android gingerbread. The gingerbread device cannot successfully make ajax requests to the service API. No other device appears to have this problem. I always get back:
HTTP Error 411. The request must be chunked or have a content length.
Some searching has indicated this is usually not a device-specific error. Here is the code I am using:
var addItemToList = function (itemId, successCallback, errorCallback) {
var address = appContext.createEndPointAddress("/List/" + itemId);
$.ajax({
type: 'PUT',
url: address,
dataType: "json",
success: successCallback,
error: errorCallback,
headers: {
"Authorization": appContext.getAuthHeader(),
Accept:
"application/site.error-ver1+json, application/site.success-ver1+json"
}
});
};
I am assuming it has to do with the request header formed by whatever browser gingerbread uses. What should I do to repair the faulty header?
Fixed the issue. Apparently, PUT requests are not normally created without a body. Thus, adding the line:
data: "{}",
fixed the problem.

call local .asmx webservice from phonegap android app to get data?

i m creating one phonegap test application, for that i want data from database through webservice (.asmx). i m just newer for this technology. i have spent 2 days for this call posible bt couldnt.
function test()
{
jQuery.support.cors = true;
$.mobile.allowCrossDomainPages = true;
$.ajax({
data: "{}",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "http://localhost:xxxxx/yyyy/testservice.asmx?op=testfunction",
success: function (msg) {
$('#divToBeWorkedOn').html(msg.text);
},
error: function (e) {
$('#divToBeWorkedOn').html("unavailable");
}
});
}
Its local service.
I would replace http://localhost:xxxxx/ with the actual internal IP address of the server (probably something like http://192.168.x.x:xxxxx/) - just make sure your server and mobile device are connected to the same network through wi-fi not gsm network.
Also make sure you updated the domain white list. more info how to do that can be found here http://docs.phonegap.com/en/2.2.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide
hope this will help, good luck!

Categories

Resources