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!
Related
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
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?
I'm developing a mobile application based on PhoneGap. I need to use AJAX, I'm working with jQuery.
I'm trying to get a JSON from a WS on PHP with CORS, but I get an error, and the error message is empty. I tested the code in a Web Browser and it works. But, when I used it with PhoneGap it did not worked. I tested my code in PhoneGAp with Fecebook WS and it works. This is the combination that I tested:
Test 1
Source: file:///C:/.../index.html (Web Browser)
WebService: http: //localhost/.../getemployees.php
Result: it works
Test 2
Source: PhoneGap
WebService: http: //localhost/.../getemployees.php
Result: it does not works
Test 3
Source: PhoneGap
WebService: https: //graph.facebook.com/OldemarshCr
Result: it works
This is the code:
$.getJSON(url, function (data) {console.log(data);})
.success(function() { console.log("second success"); })
.error(function(jqXHR, textStatus, errorThrown) {
console.log('******* '+"error: " + textStatus+' *******');
});
This is the JSON response:
{"items":[{"id":"10","firstName":"Kathleen","lastName":"Byrne","title":"Sales Representative","picture":"kathleen_byrne.jpg","reportCount":"0"},{"id":"9","firstName":"Gary","lastName":"Donovan","title":"Marketing","picture":"gary_donovan.jpg","reportCount":"0"},{"id":"7","firstName":"Paula","lastName":"Gates","title":"Software Architect","picture":"paula_gates.jpg","reportCount":"0"]}
Thanks,
In ajax call, try and add
$.ajax({
crossDomain: true,
xhrFields: {withCredentials: true}
});
$.ajax did not work... this is the error that I get
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
I tested the code on web browser and it worked.
This is the code:
$.ajax({
crossDomain: true,
xhrFields: {withCredentials: true},
type: 'GET',
url: url,
dataType: 'json',
success: function(response){ console.log(response); }
error: function(error){ console.log('Error: '+error); }
});
Thanks,
Good News!!
I solved the problem. Both methods, getJSON and ajax, are working, the problem was the server.
When I accessed the Web Services from a external server (not localhost) it worked.
Thanks,
In my android phonegap app,sometimes i get the application error as
Note when moving from one page to another page am getting this error
**"A network error occurred.(file:///android_asset/www/home/home.html?userid=91)"**
and app gets force closed .Please guide me to solve this problem.I am also attaching the screenshot.Its cuming in android 4.x versions
update
$.ajax({
cache: false,
async: true,
type: "POST",
dataType: "json",
url:url +"Status",
data: '{"NO" : "' + no}',
contentType: "application/json;charset=utf-8",
success: function (r)
{
window.open("../index/index.html?id="+id);
},
error: function (e)
{
alert("No Network");
}
});
There is a bug in the Android 4.x OS when passing parameters along the URL:
https://code.google.com/p/android/issues/detail?id=17535
https://code.google.com/p/android/issues/detail?id=17327
I written a android mobile application in JQuery Mobile and PhoneGap in Eclipse. In the application I am calling a jquery ajax to load list of data from other domain.
My jquery ajax call code is:
$.ajax({
type: "POST",
url: WEBSERVICE_URL,
async: false,
data: dataString,
dataType: 'json',
crossDomain: true,
success: function(data) {
loginData = new Object(data);
hideActivityIndigator();
if(loginData.success == "true"){
$.mobile.changePage("#selectionScreen", "slide", false, true);
} else {
$("#message_ajax").html("Invalid UserName/Password.");
}
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
hideActivityIndigator();
}
});
The variable "WEBSERVICE_URL" has a other domain php service url.
On executing above code on "pageview" event I am getting following error
Error: NETWORK_ERR: XMLHttpRequest Exception 101
Any solution is there to access service from other domain in JQuery Mobile + PhoneGap...?
I think this might solve your problem.
If you are using Phonegap/Cordova you should be able to call cross-domain web-services.. are you using an emulator or a phone?
As for emulator I would recommend Ripple, you can add it as a Chrome extension or Download the standalone version