Answer the question
In order to leave comments, you need to log in
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
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.
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);
}
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 questionAsk a Question
731 491 924 answers to any question