如何从我的Android应用程序传递数据并在我的ASP.NET MVC网站上接收它?

时间:2016-04-04 15:22:33

标签: android asp.net asp.net-mvc database transmission

我想从我的Android应用程序发送一些varibeles到我的ASP.NET网站,所以我可以在那里使用它,我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

如果您的ASP.NET应用程序具有允许其与外部应用程序交互的某种类型的公共API,您应该能够向其发出Web请求并发布所需的适当值。

我对Android语法并不十分熟悉,但像this one on making HTTP GET/POST requests from Android这样的示例应该指向正确的方向:

 // Build your client
 HttpClient httpClient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost("your-asp-mvc-application/Home/AcceptData"); 

 // Build a collection of data that you want to send
 List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
 nameValuePair.add(new BasicNameValuePair("username", "test_user"));
 nameValuePair.add(new BasicNameValuePair("password", "123456789"));

 // Encoding POST data
 try 
 {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
 } 
 catch (UnsupportedEncodingException e) {
    // log exception
    e.printStackTrace();
 }

 // Make the request
 try 
 {
    HttpResponse response = httpClient.execute(httpPost);
    // write response to log
    Log.d("Http Post Response:", response.toString());
 } 
 catch (ClientProtocolException e) 
 {
    // Log exception
    e.printStackTrace();
 } 
 catch (IOException e) 
 {
    // Log exception
    e.printStackTrace();
 }

基本上,一旦你发出了make请求,你应该能够定位你的应用程序并创建一个实际上可以接受你发送它的控制器动作:

public ActionResult AcceptData(string username, string password)
{
      // Do something here
}

答案 1 :(得分:0)

First, what form of ASP.NET are you using - Forms or MVC? Also, what do you mean by "send?" Where exactly do you want the data to end up and what exactly do you want your ASP.NET application to do once it receives the data? If you simply mean that you're creating data in your phone and you want your ASP.NET web site to be able to access it too, you can just insert the data into a database that your ASP.NET web site also has access to (e.g. through a web service call or something like that).