什么是android替代$.Ajax与数组
What is android replacement for $.ajax with array
我有现有的jquery代码提交数组'array'作为json var在登录过程
var array1 = {
"command" : "login",
"username": $("#txt_username").val(),
"password": $("#txt_password").val(),
"remember": $('#chk_remember').is(':checked')
};
$.ajax({
url: 'functions.php',
data : {'array': array1},
dataType: 'json',
type: 'POST',
success: function(data) {
if (data.success == 1 ){
$('#result').html("You have been logged in.</br>You will be redirected to another page");
}
});
and at PHP
if (isset($_POST["array"])) {
$array = $_POST['array'];
switch ($array["command"]) {
case "login" :
if (DoLogin($array["username"], $array["password"]) == true) {
$success = 1;
}
else {
$success = 0;
}
$arr = array("success" => $success, "redirect" => 1);
echo json_encode($arr);
break;
}
如何处理这个登录过程从android?
public class Utils {
public static String POST(String url, Login login){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("command", login.command);
jsonObject.accumulate("username", login.username);
jsonObject.accumulate("password", login.password);
jsonObject.accumulate("remember", login.remember);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Login object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(login);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
}
// Model login obj
public class Login {
public String password;
public String username;
public String command;
public boolean remember;
}
到
Login login = new Login();
// get reference to the views
login.username = (EditText) findViewById(R.id.user);
login.password = (EditText) findViewById(R.id.pass);
login.command = (EditText) findViewById(R.id.comm);
login.remember = ((CheckBox) findViewById(R.id.rem)).isChecked();
Utils.POST(stringUrl, login) // use AsynckTask for this http://stackoverflow.com/questions/8829135/android-http-request-asynctask
我建议使用Volley来处理你的web请求。这是一个方便的入门指南:http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
相关文章:
- 如何在Javascript中解析AJAX数组的特定部分
- 访问$.ajax()函数中的两个不同数组
- 作为一个二维数组,从ajax接收
- 如何在不使用ajax的情况下将pair值添加到数组并发送到django中的视图
- 将数组从ajax传递到python(Tornado)
- ajax成功地将数组中的数据放入表中各自的输入中
- jQuery Ajax数组序列化错误
- AJAX jquery json将数组发送到php
- 如何显示从Ajax请求返回的数组.使用Yii2
- 后关联数组使用jquery ajax
- 将ajax数组缓冲区响应绘制到画布中
- 使用Javascript数组或JSON来存储AJAX请求的返回值,以备将来使用
- 通过iron-ajax初始化对象数组(链接到caller's元素的响应)
- Ajax数组只返回1个值
- ajax数组上的PHP序列化不起作用
- 如何调用php json编码传递的ajax数组?zend框架2
- 我应该将php json编码转换为Ajax数组,第一次对我来说很难理解
- 通过JSON AJAX数组访问数据->对象
- 如何创建一个ajax数组和修改它没有回调函数
- 如何使用 Java 模拟 $.ajax 数组 POST 请求