【问题标题】:Error connect android to asp.net core web api将android连接到asp.net core web api时出错
【发布时间】:2020-11-09 15:28:41
【问题描述】:

我正在为我的 android 使用 asp.net core web api。在我的 android 内部,我正在使用 volley 连接到我的 web api,我的问题是我的 android 没有连接到我的 web api。这是我的代码。 asp.net核心

namespace MywebApplicationIkeaExample.Controllers.api
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        private readonly IDetialRepository _detialRepository;
        private readonly ICareInstructionRepository _careInstructionRepository;
        private readonly IProductRepository _productRepository;
        private readonly IMapper _mapper;

        public ProductsController(IDetialRepository detialRepository,ICareInstructionRepository careInstructionRepository,IProductRepository productRepository,IMapper mapper)
        {
            _detialRepository = detialRepository;
            _careInstructionRepository = careInstructionRepository;
            _productRepository = productRepository;
            _mapper = mapper;
        }
        public async Task<IActionResult> GetCareInstruction(int id)
        {
            var careInstruction = await _productRepository.GetCareInstruction(id);
            return Ok(careInstruction);
        }
        [HttpGet("GetProduct/{id}")]
        public async Task<IActionResult> GetProduct(int id)
        {
            var product = await _productRepository.GetProduct(id);
            var productreturn = _mapper.Map<ProductDTO>(product);
            return Ok(productreturn);
        }
        [HttpGet("GetDetails")]
        public async Task<IActionResult> GetDetails()
        {
            var Mydetail = await _productRepository.GetDetials();
            //var t = Mydetail.OfType<Detial>();
            var Mydetailreturn = _mapper.Map<IEnumerable<DetialDTO>>(Mydetail);
            return Ok(Mydetailreturn);
        }

机器人 配置

public class Config {
    static String ip="http://10.0.2.2:5000/api/";
  //   static String Regester=ip+"Register";
     static String Detial=ip+"Products/GetDetails";
}

WebApiHandler

public class WebApiHandler {
    Context context;
    String apiLink="";
    public  WebApiHandler(Context context)
    {
        this.context=context;
    }

    public void apiConnect(String Type)
    {
        switch (Type)
        {
            case "Detial":
                apiLink=Config.Detial;

                break;
        }
        StringRequest request=new StringRequest(Request.Method.GET, apiLink, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJson(response);
                Toast.makeText(context,response, Toast.LENGTH_SHORT).show();
            }
        },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
        RequestQueue requestQueue=Volley.newRequestQueue(context);
        requestQueue.add(request);
    }

    private void showJson(String response) {
    }


}

如果您看到我从邮递员那里发送请求并得到响应,但我可以在我的 android 项目中得到这个结果 enter image description here

【问题讨论】:

  • 您忽略了错误响应。看看它告诉你什么
  • 当我调试它时,它不会进入它,所以如果我放烤面包也没关系

标签: android android-volley asp.net-core-webapi


【解决方案1】:

更新:这不再是错误或解决方法,如果您的应用针对 API 级别 28 (Android 9.0) 或更高版本并使用适用于 Android 16.0.0 或更低版本的 Google Maps SDK(或者如果您的应用使用Apache HTTP Legacy 库)。它现在包含在官方文档中。公共问题已按预期行为关闭。

这是 Google Play 服务端的一个错误,在修复之前,您应该能够通过将其添加到标签内的 AndroidManifest.xml 中来解决此问题:

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

任何在他们的 android 中使用 asp.net core for web api 的人都必须使用此代码

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 2019-07-11
    • 2023-02-22
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2019-05-12
    • 2021-12-23
    相关资源
    最近更新 更多