使用来自android app的REST(POST)API登录验证

问题描述 投票:0回答:1

我已经获得了创建登录和注册页面的任务,该页面可通过应用程序与REST身份验证一起使用。我已经创建了登录和注册页面。在环顾四周并决定使用凌空之后,我在后端http://testing.universalproject.in/api/signin上运行了一个REST API,该API接受2个请求参数mobile_no和password。它将返回以下响应。

2来自EditTexts的输入和登录按钮上的验证单击,我尝试了以下操作,但不起作用。谢谢

loginButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                String phonenumber = phno.getText().toString().trim();
                String password = pass.getText().toString().trim();

                System.out.println(phonenumber + password);
                authenticateUsingServer(phonenumber, password);
            }

        });
public void authenticateUsingServer(final String mnumber, final String mPassword) {
        try {
            String URL_LOGIN = "http://testing.universalproject.in/api/signin";
            Log.e(TAG, "Authenticate using remote server");
            // Instantiate the RequestQueue.
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            StringRequest strReq = new StringRequest(Request.Method.POST,
                    URL_LOGIN, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e(TAG, "Login Response: " + response);
                    System.out.println(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Login Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Log.e(TAG, "Inside getParams");

                    // Posting parameters to login url
                    Map<String, String> params = new HashMap<>();
                    params.put("mobile_no", mnumber);
                    params.put("password", mPassword);

                    return params;
                }

            };
            // Adding request to request queue
            queue.add(strReq);
            Thread.sleep(2000);


        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
{
"token":"etc",
"user":{
"_id":"5as6d46a5s1d6a15sa",
"mobile_no":"123456789",
"status":1,
"dob":"2000-01-01",
"gender":"m",
"name":"User"
}
}

我已经使用了Controller.java


import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class Controller extends Application {
    public static final String TAG = Controller.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static Controller mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized Controller getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

LOGCAT

2020-04-11 16:24:50.134 3363-3363/com.darkabhi.app E/LoginActivity: Authenticate using remote server
2020-04-11 16:24:50.155 3363-4968/com.darkabhi.app D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-04-11 16:24:50.157 3363-4968/com.darkabhi.app E/LoginActivity: Inside getParams
2020-04-11 16:24:52.147 3363-3363/com.darkabhi.app I/Choreographer: Skipped 119 frames!  The application may be doing too much work on its main thread.
2020-04-11 16:24:52.157 3363-3401/com.darkabhi.app I/OpenGLRenderer: Davey! duration=2008ms; Flags=0, IntendedVsync=46593450818, Vsync=48576784072, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=48593305900, AnimationStart=48593333700, PerformTraversalsStart=48599401600, DrawStart=48599790600, SyncQueued=48599855200, SyncStart=48600959100, IssueDrawCommandsStart=48601051200, SwapBuffers=48601736200, FrameCompleted=48603137900, DequeueBufferDuration=202000, QueueBufferDuration=526000, 
2020-04-11 16:24:52.158 3363-3363/com.darkabhi.app E/LoginActivity: Login Error: java.io.IOException: Cleartext HTTP traffic to testing.universalproject.in not permitted
java android rest authentication android-volley
1个回答
0
投票

E / LoginActivity:登录错误:java.io.IOException:明文HTTP不允许进入testing.universalproject.in的流量

存在明文流量问题。有多个选项可解决此问题:

  1. andoird 9(Pie),以明文格式向服务器发送数据受到限制。由于文本清晰,当数据从客户端传输到服务器时,可能会窃听和篡改内容。因此,通过从病房中的android 9采取预防措施,他们现在坚持在http://上使用https://。

考虑到这种安全威胁,如果我们仍然希望允许以明文形式进行通信,那么我们必须在清单文件中明确进行。

<application
    android:usesCleartextTraffic="true"
  1. android 7(Nougat)提供了一个更好的选项,我们可以允许某些域仅以明文进行通信。我们可以通过提供network_security_config.xml(网络配置文件)来实现。

    your_domain.com

在res / xml下添加网络安全配置文件,并保持cleartextTrafficPermitted = true

之后,我们必须将此文件添加到清单中。

<application
    android:name=".MyApplication"
    android:networkSecurityConfig="@xml/network_security_config"
...

最后,建议的仅通过明文(http://)与安全通信(https://)进行通信的方法。

© www.soinside.com 2019 - 2024. All rights reserved.