【问题标题】:GET request based on UserName基于 UserName 的 GET 请求
【发布时间】:2018-11-04 15:51:57
【问题描述】:

我正在向我的 android 应用程序添加用户登录,以提高安全性并识别用户。我有一个登录屏幕,用户首先从下拉列表中选择他们的名字。然后将他们的选择存储在一个名为userName 的变量中。

然后我有 6 个框,用户可以在其中输入他们唯一的 pin,并将其保存到名为 userPin 的变量中。

我有一个 oracle 数据库表,其中包含每个员工的用户名和 pin,并且当前有一个 REST 服务,我正在使用该服务将数据从我的应用程序发送到同一架构中的不同表。

我的问题是...我如何首先执行 GET 请求以从我的 oracle 数据库中获取与用户选择的用户名相关联的 userPin?

任何帮助将不胜感激!

代码尝试:

  private class AsyncTaskRunner extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
            String url = "http://xxx.xx.xxx.xx:xxxx/engAppApi/webservices/userTable/Employee_Name=" + employee;

            URL object = new URL(url);

            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("GET");

            if (con.getResponseCode() == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
                while ((line=br.readLine()) != null) {
                    userPinRetrieved+=line;
                }
            }

        } catch (Exception e) {
            Log.v("ErrorAPP", e.toString());
        }
        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }
}

}

【问题讨论】:

  • 分享您的代码并解释您卡在哪里以及您遇到的错误是什么
  • @Thunder 代码添加

标签: java android rest login get


【解决方案1】:

你的问题不是很清楚,你应该包括

  1. 如何调用 REST 请求。
  2. 您为实现这一目标所做的任何努力。
  3. 您遇到了什么错误。

对于调用任何 REST 请求(GET、POST、PUT、DELETE),您可以使用 android 库,这将使您的工作变得非常简单和高效。一些最常用的带有示例的库如下所示:-

1.Retrofit
我对改造不太熟悉,所以这里有一个你可以关注的链接
Tutorial For Retrofit

2.Volley
Volley 由 Google 开发,并被程序员广泛用于开发 Android 应用程序。

调用 GET 请求的示例:-

dependencies {
    ...
    compile 'com.android.volley:volley:1.1.0'
}

上面的代码用于将库添加到您的项目中。

String url = "http://xxx.xx.xxx.xx:xxxx/engAppApi/webservices/userTable/Employee_Name=" + employee;

 //creating a string request to send the request to the URL
    StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                //Printing Your Response To LOGCAT
                 Log.d("VolleyResponse","Response:- "+response);    
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                 //Printing Your Error Response To LOGCAT
                 Log.e("VolleyResponse","Error Response:- "+error.getMessage());
                }
            });

    //creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //adding the string request to request queue
    requestQueue.add(stringRequest);

有关 Volley 的更多信息,您可以关注 This 链接。

【讨论】:

  • 您好,感谢您的评论,我会更新我想要实现的目标
  • 那么上面,我正在尝试获取与从下拉菜单中选择的任何员工姓名相关联的 pin?
  • 尝试上面的编辑代码,确保替换您的 URL 并添加依赖项和 INTERNET 权限。 @AndroidBeginner2018
  • 那会找回密码吗?
  • 你最好尝试一下并检查 logcat 的响应
猜你喜欢
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 2019-04-12
  • 2016-03-02
  • 1970-01-01
  • 2015-07-25
相关资源
最近更新 更多