关于 javascript:什么是 android 用数组替换 $.ajax | 珊瑚贝

What is android replacement for $.ajax with array

本问题已经有最佳答案,请猛点这里访问。


我有现有的 jquery 代码可以在登录过程中将数组 \\’array\\’ 作为 json var 提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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”);
}
});

在 PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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的这个登录过程?

  • 为什么不直接使用 $.ajax() ?
  • 这是一个javascript,但我需要在Android代码中重复这个过程
  • 如果您在 Java 应用程序中建立连接,则需要研究如何做到这一点。我会从搜索”cURL post Java Android”开始,这应该会为您提供很多很好的研究链接。
  • 哦,您的意思是您正在编写一个独立的 Android 应用程序,而不是一个 Web 应用程序。好吧,这是完全不同的事情。
  • 我已经知道如何通过 Json 发送字符串变量列表,但我需要的是通过 JSON 编码和解码数组对象


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
}

进入活动

1
2
3
4
5
6
7
8
9
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

  • 将不起作用,因为 PHP 代码首先在帖子中查找名为”array”的数组,然后在数组中查找”command”项,并且用户/密码项在数组中


我建议研究使用 Volley 来处理您的网络请求。这是一个方便的入门指南:http://www.androidhive.info/2014/05/android-working-with-volley-library-1/


来源:https://www.codenong.com/25275092/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?