Home>

I am making an app with android studio, but I would like to use LINE notify to send a fixed message to LINEh on my smartphone when I press a button on the screen of my own app.
I finish writing the code and run it, but it doesn't work.
It seems that the call from another class isn't working, but I can't figure out what's wrong. .. ..
I haven't tried to call another class before, so I'm not used to it, but I'd appreciate it if you could point out any mistakes or mistakes.
I'm sorry, but thank you.

--MainActivity.java--
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    private LineNotify classln;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        classln = new LineNotify ();
        Button button = findViewById (R.id.button);
        button.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                String str = classln.name ();
    }
}
--LineNotify.java--
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Objects;
import java.util.stream.Collectors;
public class LineNotify {
    public static void main (String [] args) {
        String Token = "**** Put an access token here ****";
        LineNotify lineNotify = new LineNotify (token);lineNotify.notify ("line test");
        lineNotify.notify ("Test signal");
    }
    private final String token;
    public LineNotify (String token) {
        this.token = token;
    }
    public void notify (String message) {
        HttpURLConnection connection = null;
        try {
            URL url = new URL ("https://notify-api.line.me/api/notify");
            connection = (HttpURLConnection) url.openConnection ();
            connection.setDoOutput (true);
            connection.setRequestMethod ("POST");
            connection.addRequestProperty ("Authorization", "Bearer" + token);
            try (OutputStream os = connection.getOutputStream ();
                 PrintWriter writer = new PrintWriter (os)) {
                writer.append ("message =") .append (URLEncoder.encode (message, "UTF-8")). flush ();
                try (InputStream is = connection.getInputStream ();
                     BufferedReader r = new BufferedReader (new InputStreamReader (is))) {
                    String res = r.lines (). Collect (Collectors.joining ());
                    if (! res.contains ("\" message \ ": \" ok \ "")) {
                        System.out.println (res);
                        System.out.println ("failure");
                    }
                }
            }
        } catch (Exception ignore) {
        } finally {
            if (connection! = null) {
                connection.disconnect ();
            }
        }
    }
    @Override
    public boolean equals (Object o) {
        if (this == o) return true;if (o == null || getClass ()! = o.getClass ()) return false;
        LineNotify that = (LineNotify) o;
        return Objects.equals (token, that.token);
    }
    @Override
    public int hashCode () {
        return Objects.hash (token);
    }
    @Override
    public String toString () {
        return "LineNotify {" +
                "token ='" + token +'\'' +
                '}';
    }
}
--Manifest.xml--<? xml version = "1.0" encoding = "utf-8"?><manifest xmlns: android = "http://schemas.android.com/apk/res/android"
    package = "com.e.testsignalapplication"><uses-permission android: name = "android.permission.INTERNET"></uses-permission>                    <intent-filter>                            <category android: name = "android.intent.category.LAUNCHER" />        </intent-filter>    </activity></application></manifest>
  • Answer # 1

    I have no knowledge about LINE notify, so I can't say anything about it, but there is a big problem with how to create a program. Unlike ordinary Java programs, programming of Android applications created using the Android SDK is different.public static void main ()You cannot create a program that starts with. It is necessary to modify the method of creating a program according to the method of SDK.

    Also, as a constraint on Android application development, it is not possible to perform network communication with the thread that operates the UI (main thread).ThreadIt is necessary to process in another thread for network communication using. In addition, if you check the method of another thread processing by programming the Andoroid applicationAsyncTaskI think that there are many hits using this class, but this class has been deprecated at API level 30, so you should use another method.


    Where should I write the Thread code itself to be processed in another thread?

    It depends on the design, but I think it should be described in the LineNotify class.

    As an example

    class LineNotify {
        private static final String token = "**** Put the access token here ****";
        public void notify (String message) {
            Comm t = new Comm (message);
            t.start ();
        }
        static class Comm extends Thread {
            private String message;
            Comm (String message) {
                this.message = message;
            }
            @Override
            public void run () {
                // Describe the network communication processing in the current Notify () method here
            }
        }
        (Omitted below)
    }

    On the side of MainActivity.java to call

          button.setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick (View v) {
                    classln.notify ("line test");
                    classln.notify ("Test signal");
                }
            });

    I think you can set the character string you want to send in the argument of notify () like this.