A
A
Amir Kenesbay2022-02-08 22:50:47
Java
Amir Kenesbay, 2022-02-08 22:50:47

How to read a search query from the console and display the result of a Wikipedia search?

Explain the task, since I am still new to Java, I can not understand the essence of the task. I read about HttpURLConnection and worked a little with its methods and made get, put requests.

Task:

Write a program that reads a search query from the console and displays the result of a Wikipedia search. The task is divided into 4 stages:
Read the request
Make a request to the server
Parse the response
Print the result

The first and fourth points do not need much explanation, let's focus on the request to the server.

This task can also be divided into several stages:

Generating a request
Request to the server
Preparing to process the response
Processing the response

Consider this in more detail:

Generating an
API request provides the ability to make search queries without keys. Like this, approximately, in a way:

You can open this link in a browser and look at the result of the request.
However, in order for the request to succeed, invalid characters should be removed from the link, that is, Percent-encoding, aka URL Encoding.
To do this in Java, you can use the static encode method in the URLEncoder class, like this:
street = URLEncoder.encode(street, "UTF-8");
That's it, the URL is ready! It remains now to make a request to the server ...

Request to the server
For GET and POST requests, you can use the HttpURLConnection class. This is the simplest. Just create, open a connection and get an InputStream. We don't even need to read it, Gson will do it for us.
You can also use retrofit, or something similar.

Preparing to process the response
The server returns data in JSON format.
But we do not need to parse it manually, there is a Gson library from Google for this.
There are examples here:
https://github.com/google/gson
https://habrahabr.ru/company/naumen/blog/228279/

If there is time left, you can write getting the article selected during the search, and so on.


That is, I made such a get request, and then what?

String urlAddress = "https://ru.wikipedia.org/w/api.php?action=query&list=search&utf8=&format=json&srsearch=\"Java\"";
        HttpURLConnection connection = null;

        URL url = null;
        InputStreamReader isR = null;
        BufferedReader bfR = null;
        try {
            url = new URL(urlAddress);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(200);
            connection.setReadTimeout(200);
            connection.connect();

            if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
                isR = new InputStreamReader(connection.getInputStream());
                bfR = new BufferedReader(isR);
                String line;
                while ((line = bfR.readLine()) != null) {
                    System.out.println(line);
                }
            } else {
                System.out.printf("Fail %s", connection.getResponseCode());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                isR.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bfR.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-02-08
@xez

Urgently read about try-with-resources!
Your task should consist of the following methods:

// Считать запрос
String request = getRequest();

// Сделать запрос к серверу
String response = getResponseFromWiki(request);

// Распарсить ответ
(?) result = parseResponse(response);

// Вывести результат
showResult(result);

You apparently coped with the request to the server.
You need to do all this with a separate method (of the getResponseFromWiki type), which will return the received string (or whatever you get back)
Well, then according to the scheme.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question