A
A
Alepan2017-05-31 15:52:36
Android
Alepan, 2017-05-31 15:52:36

How to pass parameters in HTTPUrlConnection GET request?

Hello!
The www.umori.li website provides a REST API that can be used to get a list of quotes. For example, the response to the request " www.umori.li/api/get?site=bash.im&name=bash&num=100 " will be a JSON array with 100 quotes. To start learning REST, I decided to use HttpURLConnection, and then proceed to retrofit, but immediately ran into a problem. Instead of these 100 quotes, I get this:

<html><head><title>www.umori.li</title></head><frameset BORDER='0' frameborder='0' framespacing='0' rows='100%,*'>
                                                                     <frame name='target' src='http://umorili.herokuapp.com/api/get?site=bash.im&name=bash&num=100'>
                                                                     <noframes>  <body BGCOLOR='#FFFFFF'>
                                                                     This page requires that your browser supports frames.
                                                                     <BR>You can access the page without frames with this <a href='http://umorili.herokuapp.com/api/get?site=bash.im&name=bash&num=100'>link</A>.
                                                                     </body></noframes></frameset></html>

Although if you enter the string " www.umori.li/api/get?site=bash.im&name=bash&num=100 " in the browser, you can see the same JSON array with quotes. I don’t quite understand what the error could be, but I think that the parameters are being passed incorrectly.
Class code:
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class RestHelper extends Thread {

    URL url;
    HttpURLConnection httpURLConnection;

    @Override
    public void run() {
        BufferedReader in = null;
        StringBuilder buf;
        String urlString = null;

        try {
            url = new URL("http://www.umori.li/api/get?site=bash.im&name=bash&num=100");
            httpURLConnection = (HttpURLConnection)url.openConnection();
        } catch (MalformedURLException e) {
            Log.i("REST", "MalformedURLException");
        } catch (IOException e) {
            Log.i("REST", "IOException");
        }

        try {
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.connect();
        } catch (ProtocolException e) {
            Log.i("REST", "ProtocolException");
        } catch (IOException e) {
            Log.i("REST", "IOException");
        }

        buf = new StringBuilder();
        try {
            in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                buf.append(line + "\n");
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("REST", "INPUT DATA:" + buf.toString());

        httpURLConnection.disconnect();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alepan, 2017-05-31
@Alepan

Understood)
Maybe someone will come in handy
1. You need to remove httpURLConnection.setDoOutput(true); because it is used only for POST requests
2. By " www.umori.li/api/get?site=bash.im&name=bash&num=100 " it returns a page with frames containing quotes. Because I do not support frames, only the page code is returned to me. To get pure JSON, you need to use " umorili.herokuapp.com/api/get?site=bash.im&name=ba... " (which is basically what he wrote me in the answer).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question