从Android Studio视图中调用Asp.net Web API

编程入门 行业动态 更新时间:2024-10-28 14:32:16
本文介绍了从Android Studio视图中调用Asp Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

通过android studio项目访问asp Web API时遇到问题.我的Web API通过实体框架与数据库连接.我想通过android商人视图通过API Merchant Controller调用商人列表.这是我的商人的HttpGet方法:

I have a problem while accessing asp web API through android studio project. My web API connect with the database through Entity Framework. I want to call the list of Merchants through API Merchant Controller from android merchant view. Here is my HttpGet method for Merchant:

public class MerchantController : ApiController { private DostiCardDBEntities merchantEntities = new DostiCardDBEntities(); [HttpGet] public HttpResponseMessage listOfMerchant() { return Request.CreateResponse(HttpStatusCode.OK, merchantEntities.MerchantTables.ToList()); } }

我通过AsyncTask doInBackground方法i-e访问商家列表

I access list of Merchants through AsyncTask doInBackground method i-e

private class ExecuteTask extends AsyncTask<String, Integer, String>{ String jsonText = ""; HttpsURLConnection connection; @Override protected String doInBackground(String... strings) { try { URL url = new URL("169.254.80.80:6040/api/Merchant"); connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); InputStream inputStream = connection.getInputStream(); int byteCharacter; while ((byteCharacter = inputStream.read()) != -1){ char c = (char) byteCharacter; jsonText += c; } } catch (IOException e) { e.printStackTrace(); } finally { connection.disconnect(); } return null; } @Override protected void onPostExecute(String s) { Toast.makeText(getApplicationContext(), jsonText, Toast.LENGTH_LONG).show(); } }

推荐答案

您应该将HttpsURLConnection更改为HttpURLConnection,因为您的URL使用协议http而不是https

you should change HttpsURLConnection to HttpURLConnection because your URL using protocol http not https

更多推荐

从Android Studio视图中调用Asp.net Web API

本文发布于:2023-10-29 13:29:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   Studio   Android   Asp   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!