简体   繁体   中英

How to send data from Android to server and from server to Android

I am developing an app. How to send data from app to server and then server automatically send data to next app?

you can create a web service on the server side and call it from android client to send and recieve data, or you write a servlet.

A web service is a standard for exchanging information between different types of applications irrespective of language and platform. For example, an android application can interact with java or .net application using web services.

Example on Android side:

URL url = null;
try {
    String registrationUrl = String.format("http://myserver/register?id=%s&name=%s", myId, URLEncoder.encode(myName,"UTF-8"));
    url = new URL(registrationUrl);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    int responseCode = httpConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        Log.d("MyApp", "Registration success");
    } else {
        Log.w("MyApp", "Registration failed for: " + registrationUrl);              
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

You can simply use Volley library for networking related operations inside your android application. It'll be pretty easier for you to use. Try this link to learn the basics: https://developer.android.com/training/volley/index.html

For server side operation you need to create a web service.

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