基于UserName的GET请求

时间:2018-05-25 09:31:54

标签: java android rest login get

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

然后我有6个框,用户输入其唯一的引脚,该引脚保存到名为userPin的变量。

我有一个oracle数据库表,其中包含每个员工的用户名和密码,目前有一个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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

  1. 您如何调用REST请求。
  2. 您已经完成的任何努力。
  3. 你得到的错误是什么。
  4. 要调用任何 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);
    

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

相关问题