admin管理员组文章数量:1323716
I am wondering how to call a function just like in jQuery, but then in Java (for a native Android app).
$.ajax({
url:".php",
method:"POST",
data:{username:uname_field,password:upass_field},
}).success(function(response){
if (response=="correct"){
alert("You are now logged in");
}
});
Above is the code for JavaScript, but I am wondering what the code would look like in Java.
Thank you!
I am wondering how to call a function just like in jQuery, but then in Java (for a native Android app).
$.ajax({
url:"http://test./read_mySQL.php",
method:"POST",
data:{username:uname_field,password:upass_field},
}).success(function(response){
if (response=="correct"){
alert("You are now logged in");
}
});
Above is the code for JavaScript, but I am wondering what the code would look like in Java.
Thank you!
Share Improve this question edited Jul 7, 2017 at 16:48 baikho 5,3684 gold badges42 silver badges49 bronze badges asked Jan 13, 2016 at 8:59 Syamsoul AzrienSyamsoul Azrien 2,7526 gold badges33 silver badges58 bronze badges 2- use asyncTask – Randyka Yudhistira Commented Jan 13, 2016 at 9:01
- As what I studied, AsyncTask is for calling another thread (Background running), so, is it are used for AJAX also? Sorry for my bad english grammar.. – Syamsoul Azrien Commented Jan 13, 2016 at 9:04
4 Answers
Reset to default 2With Android you can use OkHttp
An example from there
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
Update
Plase, read this article Using OkHttp, espessially Asynchronous Network Calls. Because of you will need to do requests asynchronously.
Refer "Using OKHttp, what is the difference between synchronous request in AsyncTask and OKhttp Asynchronous request?" for some additional notes about why preferable to using OkHttp
over AsyncTask.
AJAX - Asynchronous JavaScript and XML
As the name stated ajax belongs to JavaScript. You should use a native android library to make your requests.
I suggest to use OkHttp (Http Client) in bination with Retrofit 2 (allows easy asynchronous calls like ajax and much more).
As what I studied, AsyncTask is for calling another thread (Background running), so, is it are used for AJAX also?
No.
But any HTTP calls in new Android versions (4.x) are required to be called in AsyncTask.
And also, you need android.INTERNET permission.
Otherwise, it wouldn't work.
For more info, you can read this:
Simply Android GET & POST Requests Examples
http://codeproject./Tips/1034468/Android-Simply-Sending-HTTP-GET-POST-Requests-To-S
As already mentioned above AJAX stands for Asynchronous JavaScript and XML. To make an httpRequest in Java Android i strongly suggest Volley Library and Singleton Pattern: https://developer.android./training/volley/request
本文标签: javascriptAndroidAJAX in JavaStack Overflow
版权声明:本文标题:javascript - Android - AJAX in Java - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742125893a2421938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论