0

My aim is to make a HTTP request, and then change the textview that I have to be that data we got back.

The button changes the text to "Once again, we are receiving IO ERROR" always, and I don't know how to make it display the data that sendGet() is meant to return.

Code below.

package com.example.textchanger;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;



public class MainActivity extends AppCompatActivity {

    private static final String USER_AGENT = "Mozilla/5.0";
    private static final String GET_URL = "http://stark-stream-56750.herokuapp.com/all";

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        changeTextOnce();
    }

    private static String sendGET() throws IOException {
        // Make URL object.
        URL url_obj = new URL(GET_URL);
        // Connect to object.
        HttpURLConnection url_con = (HttpURLConnection) url_obj.openConnection();
        // Set Request Data
        url_con.setRequestMethod("GET");
        url_con.setRequestProperty("User-Agent", USER_AGENT);

        // What response code did we get back?
        int responseCode = url_con.getResponseCode();

        // If good...
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // Make a way to read the data, and then read it
            BufferedReader inReader = new BufferedReader(new InputStreamReader(url_con.getInputStream()));

            // Can't be modified.
            String inputLine;
            // A string buffer is like a String, but can be modified.
            StringBuffer response = new StringBuffer();

            // For each line in the data we have, until we have no more lines
            while ((inputLine = inReader.readLine()) != null) {
                // Add the data to the string
                response.append(inputLine);
            }

            // Close the reader
            inReader.close();

            // Print result
            System.out.println(response.toString());
            return response.toString();
        } else {
            // Error
            System.out.println("GET request not worked");
            return "Error!";
        }
    }

    private void changeTextOnce() {
        final TextView changingText = (TextView) findViewById(R.id.change_this);
        Button changingButton = (Button) findViewById(R.id.change_text_button);

        changingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    changingText.setText(sendGET());
                } catch (IOException e) {
                    changingText.setText("Once again, we are recieving IO ERROR");
                }
            }
        });
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

Fix: Make it https not http link.

Resources for the next error you may encounter: NetworkOnMainThreadException