为什么Android射击库无法连接ASP.NET Web API?

发布于 2025-01-23 13:41:34 字数 4085 浏览 0 评论 0原文

我正在尝试使用射击库从Android客户端调用ASP.NET Web API。 .NET API工作正常,听力端口是: http:// localhost:5000和https:// localhost:5001在我的本地计算机中。

但是,当我尝试运行Android客户端时,将基本URL保持为 http:/10.0.2.2:5000 /api/login 我在Console->中发现以下错误。

04/23 23:13:41: Launching 'app' on Nexus 6 API 24.
Install successfully finished in 9 s 435 ms.
$ adb shell am start -n "com.example.tourexpwip/com.example.tourexpwip.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 3662 on device 'Nexus_6_API_24 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Late-enabling -Xcheck:jni
W/art: Unexpected CPU variant for X86 using defaults: x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.tourexpwip-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/System.out: URL: http://10.0.2.2:5000/api/login/
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/: HostConnection::get() New Host Connection established 0xa67ec7c0, tid 3662
E/Volley: [210] NetworkUtility.shouldRetryException: Unexpected response code 307 for http://10.0.2.2:5000/api/login/
D/: HostConnection::get() New Host Connection established 0xa67ecac0, tid 3683
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xb5c05660: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xb5c05660: ver 2 0 (tinfo 0xb5c032b0)
W/System.err: com.android.volley.ServerError
        at com.android.volley.toolbox.NetworkUtility.shouldRetryException(NetworkUtility.java:201)
W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:145)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:132)
W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
I/Choreographer: Skipped 68 frames!  The application may be doing too much work on its main thread.
D/EGL_emulation: eglMakeCurrent: 0xb5c05660: ver 2 0 (tinfo 0xb5c032b0)
I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.

在ASP.NET Web api->中登录的控制器类

namespace TourExpWIP1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        NetCoreAuthenticationContext dbContext = new NetCoreAuthenticationContext();

        public string Post([FromBody] TblUser  value)
        {
            
            if (dbContext.TblUser.Any(user => user.UserName.Equals(value.UserName)))
            {
                TblUser user = dbContext.TblUser.Where(u => u.UserName.Equals(value.UserName)).First();
                var client_post_hash_password = Convert.ToBase64String(
                    Common.SaltHashPassword(
                        Encoding.ASCII.GetBytes(value.Password),
                        Convert.FromBase64String(user.Salt)));
                if (client_post_hash_password.Equals(user.Password))
                    return JsonConvert.SerializeObject(user);
                else return JsonConvert.SerializeObject("Wrong Password");
            }
            else
            {
                return JsonConvert.SerializeObject("user does not exists in db");
            }
        }
    }
}

I am trying to call asp.net web api from Android Client using Volley library. The .net API is working fine and listening ports are:
http://localhost:5000 and https://localhost:5001 in my local machine.

But,When, I try to run android client keeping the base url as http://10.0.2.2:5000/api/login
I found below error in console->

04/23 23:13:41: Launching 'app' on Nexus 6 API 24.
Install successfully finished in 9 s 435 ms.
$ adb shell am start -n "com.example.tourexpwip/com.example.tourexpwip.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 3662 on device 'Nexus_6_API_24 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Late-enabling -Xcheck:jni
W/art: Unexpected CPU variant for X86 using defaults: x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.tourexpwip-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/System.out: URL: http://10.0.2.2:5000/api/login/
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/: HostConnection::get() New Host Connection established 0xa67ec7c0, tid 3662
E/Volley: [210] NetworkUtility.shouldRetryException: Unexpected response code 307 for http://10.0.2.2:5000/api/login/
D/: HostConnection::get() New Host Connection established 0xa67ecac0, tid 3683
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xb5c05660: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xb5c05660: ver 2 0 (tinfo 0xb5c032b0)
W/System.err: com.android.volley.ServerError
        at com.android.volley.toolbox.NetworkUtility.shouldRetryException(NetworkUtility.java:201)
W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:145)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:132)
W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
I/Choreographer: Skipped 68 frames!  The application may be doing too much work on its main thread.
D/EGL_emulation: eglMakeCurrent: 0xb5c05660: ver 2 0 (tinfo 0xb5c032b0)
I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.

The controller class for login in ASP.NET web api->

namespace TourExpWIP1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        NetCoreAuthenticationContext dbContext = new NetCoreAuthenticationContext();

        public string Post([FromBody] TblUser  value)
        {
            
            if (dbContext.TblUser.Any(user => user.UserName.Equals(value.UserName)))
            {
                TblUser user = dbContext.TblUser.Where(u => u.UserName.Equals(value.UserName)).First();
                var client_post_hash_password = Convert.ToBase64String(
                    Common.SaltHashPassword(
                        Encoding.ASCII.GetBytes(value.Password),
                        Convert.FromBase64String(user.Salt)));
                if (client_post_hash_password.Equals(user.Password))
                    return JsonConvert.SerializeObject(user);
                else return JsonConvert.SerializeObject("Wrong Password");
            }
            else
            {
                return JsonConvert.SerializeObject("user does not exists in db");
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2025-01-30 13:41:34

尝试此

1.在Android清单中,在标签应用程序中添加:
内部标记应用程序添加此标签:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

<application
    android:usesCleartextTraffic="true"
    android:networkSecurityConfig="@xml/network_security_config"
...
/>

2。app/src/src/main/res/xml/的相应网络_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

更多详细信息,请从此处查找 - 链接

Try this

1.In Android Manifiest, in tag application add:
inside tag application add this tag:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

<application
    android:usesCleartextTraffic="true"
    android:networkSecurityConfig="@xml/network_security_config"
...
/>

2.With a corresponding network_security_config.xml in app/src/main/res/xml/:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

More details please find from here- Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文