Home > OS >  How can I use a variable from the same class but different method in Android?
How can I use a variable from the same class but different method in Android?

Time:03-24

I've recently picked up Java and I'm trying to make a simple application in Android Studio that can access API. I'm trying to use HttpURLConnection but I'm having issues with classes, I get errors when I try to combine the two classes and can't seem to make it work without both.

My aim is to make it so that my TextView named TextView1 will be able to be changed to display the code coming from the following.

http.getResponseCode() " " http.getResponseMessage()

CODE

package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.net.*;

public class MainActivity extends AppCompatActivity {

    public void main(String[] args) throws Exception {

        // Sending get request
        setContentView(R.layout.activity_main);

        URL url = new URL("https://reqbin.com/echo/get/json");
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        http.setRequestProperty("Content-Type", "application/json");
        http.setRequestProperty("Accept", "application/json");

        String responsecode = http.getResponseCode()   " "   http.getResponseMessage();
        http.disconnect();
    }

    public void onCreate(Bundle savedInstanceState) {

        // Sending get request
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView)findViewById(R.id.TextView1);
        String string = "Hello";
        textView.setText(string);
    }
}

CodePudding user response:

First of all, As long as I know, the "main" method are not used on Android.

for API calls, you may have better options :

  • use a framework (like most used Retrofit)
  • use Thread and update your UI through the method runOnUIThread
  • use AsyncTask (deprecated)
  • or maybe other ways

My point is you cannot run an API call on the UI Thread obviously : the thread who is trying to load you the Activity. And as far as I remember, Android is preventing that by an explicit exception.

So, what you can do is calling a new Thread for example on you onCreate method, you call there ou HttpCall, you get the response (on a String for example) then update your textView through the runOnUIThread() method. Into the runOnUIThread, you may call your textView directly if you did something like this :

TextView textView;
public void onCreate(Bundle savedInstanceState) {

    // Sending get request
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView)findViewById(R.id.TextView1);
    new Thread(); // you create your thread here
} 

On Your Thread you may read the stream (thanks to @Pablo Lanza for pointing that out) or directly do from the thread :

String responsecode = http.getResponseCode()   " "   http.getResponseMessage();

Then on the runOnUIthread, you can do :

textView.setText(responsecode);

so you can end up with a similar code like the below :

TextView textView;
public void onCreate(Bundle savedInstanceState) {

    // Sending get request
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView)findViewById(R.id.TextView1);
    new Thread(new Runnable() {
        @Override
        public void run() {
            URL url = new URL("https://reqbin.com/echo/get/json");
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestProperty("Content-Type", "application/json");
            http.setRequestProperty("Accept", "application/json");

            String responsecode = http.getResponseCode()   " "   http.getResponseMessage();
            http.disconnect();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(responsecode);
                }
            });
        }
    }).run();
}

CodePudding user response:

May be your code doesn't work because you are not reading the stream.

Try this lines before reading the response:

InputStream is=URLConnection.getInputStream();
while(is.read>=0);
  • Related