首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android Studio中使用URL调用web api函数?

如何在Android Studio中使用URL调用web api函数?
EN

Stack Overflow用户
提问于 2018-06-20 01:46:22
回答 1查看 1.4K关注 0票数 -1

我已经看过很多教程了,但是我被难住了。这是到目前为止我从几个指南中总结出来的代码:

代码语言:javascript
运行
复制
protected Boolean doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        try {
            // Create URL
            url = baseUrl + "signin?username=" + mEmail + "&password=" + mPassword + "&remember_me=true&accept_terms=true";

            // Next, we create a new JsonArrayRequest. This will use Volley to make a HTTP request
            // that expects a JSON Array Response.
            // To fully understand this, I'd recommend readng the office docs: https://developer.android.com/training/volley/index.html
            JsonArrayRequest arrReq = new JsonArrayRequest(Request.Method.GET, url,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            // Check the length of our response 
                            if (response.length() > 0) {
                                for (int i = 0; i < response.length(); i++) {
                                    try {
                                        JSONObject jsonObj = response.getJSONObject(i);
                                        String repoName = jsonObj.get("name").toString();
                                        String lastUpdated = jsonObj.get("updated_at").toString();
                                    } catch (JSONException e) {
                                        // If there is an error then output this to the logs.
                                        Log.e("Volley", "Invalid JSON Object.");
                                    }

                                }
                            } else {
                            }

                        }
                    },

                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // If there a HTTP error then add a note to our repo list.
                            Log.e("Volley", error.toString());
                        }
                    }
            );
            // Add the request we just defined to our request queue.
            // The request queue will automatically handle the request as soon as it can.
            requestQueue.add(arrReq);
            // Simulate network access.
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return false;
        }

        for (String credential : DUMMY_CREDENTIALS) {
            String[] pieces = credential.split(":");
            if (pieces[0].equals(mEmail)) {
                // Account exists, return true if the password matches.
                return pieces[1].equals(mPassword);
            }
        }

        // TODO: register the new account here.
        return false;
    }

我尝试使用的接口是:https://neurofit.me/nfoidc/api/swagger-ui#/我想在我的应用程序中实现一个登录函数,所以我想调用登录函数。我有用户输入的电子邮件和密码,但还有其他参数,如accept-language和user-agent,称为headers。

我非常困惑如何才能只发送电子邮件和密码,并得到一个JSON文件。我也不认为我构造的URL是正确的。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50934216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档