R
R
Rinefica2018-02-25 22:54:31
Java
Rinefica, 2018-02-25 22:54:31

How to organize a client-server application on android?

The server is implemented, there is an API to it. Accepts a URL, returns JSON.
You need to make an application on Android, there are no special security requirements, all its tasks are reading and writing articles, adding photos and messaging. What modern approaches are currently being used and what is suitable?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Zagaevsky, 2018-02-26
@zagayevskiy

For such api, Retrofit is ideal.

C
Comatu, 2018-02-26
@Comatu

I tried to make a similar option using Firebase: but then I couldn’t deal with it, I didn’t have enough experience. It implements all the described possibilities. As a result, I wrote it myself in pure java, everything is quite primitive. But not for large amounts of data.

A
Alexander Yudakov, 2018-02-26
@AlexanderYudakov

Plain Java and the standard Android library are fine:

/**
 * Выполняет GET запрос к http-серверу, 
 * возвращает JSONArray, либо JSONObject */
public static Object getJsonFromServer(String relativeUrl) throws Exception {
    URL url = new URL("http://example.com/" + relativeUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    int statusCode = con.getResponseCode();
    if (statusCode != HttpURLConnection.HTTP_OK)
        throw new IOException(con.getResponseMessage() + " (HTTP " + statusCode + ")");

    BufferedReader reader = new BufferedReader(new InputStreamReader(
        con.getInputStream(), Charset.forName("utf-8")));

    StringBuilder sb = new StringBuilder();
    try {
        String line;
        while ((line = reader.readLine()) != null)
            sb.append(line);
    } finally {
        reader.close();
    }

    String text = sb.toString();
    if (TextUtils.isEmpty(text))
        return null;

    return new JSONArray("[" + text + "]").get(0);
}

Nothing else is needed.

I
Ilya Pavlovsky, 2018-03-01
@TranE91

If according to the Orthodox, then now the Retrofit + RxJava approach = the most powerful tool for client-server tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question