【发布时间】: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