简体   繁体   中英

How can I pass data from my android application and receive it on my ASP.NET MVC website?

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

If your ASP.NET application has some type of public API that would allow it to interact with external applications, you should be able to make a Web Request to it and post the appropriate values that you needed.

I'm not terribly familiar with Android syntax, but an example like this one on making HTTP GET/POST requests from Android should point you in the right direction :

 // 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();
 }

Basically, once you make make requests, you should be able to target your application and create a Controller Action that can actually accept what you are sending it :

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

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 (eg through a web service call or something like that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM