I want to send a message to LINE from the program I created on Android Studio.

Asked 2 years ago, Updated 2 years ago, 46 views

Recently, I just started developing it on Android Studio, and I would like to send a test message to my line group on LINE Notify.
I have already obtained the access token on LINE side, and all I have to do is write down the code, but I don't know how to write the code, so I'm worried.

I've been looking into various things, but I'm at a loss because I can't use it because Android Studio has an error even if I write the code as it is.

I'm sorry, but I'd appreciate it if you could give me some helpful websites, tips, and reference codes.
Thank you for your cooperation.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
  public static void main(String[]args) {
    HttpSendJSON httpSendJSON = new HttpSendJSON();
    String result = httpSendJSON.callPost();
    System.out.println(result);
  }
}

classHttpSendJSON{
  private String ACCESS_TOKEN = "access token";
  private String UID = "User ID";
  private String MESSAGE = "Messages you want to send";

  public String callPost() {
    HttpURLConnection con=null;
    StringBuffer result = new StringBuffer();

    try{
      URL url = new URL ("https://api.line.me/v2/bot/message/push");
      con=(HttpURLConnection) url.openConnection();

      con.setDoOutput(true);
      con.setRequestMethod("POST");
      con.setRequestProperty("Authorization", "Bearer" + ACCESS_TOKEN);
      con.setRequestProperty("Content-Type", "application/json; charset=utf-8");

      String parameters= 
                "{" +
                String.format("\"to\":\"%s\"",", UID)+ 
                "    \"messages\":[{"+
                "        \"type\":\"text\","+
                 String.format("\"text\":\"%s\"",MESSAGE)+ 
                "    }]" +
                "}";
      System.out.println(parameters);

      OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
      out.write(parameters);
      out.flush();
      con.connect();

      final int status = con.getResponseCode();
      if(status==HttpURLConnection.HTTP_OK){
          final InputStream in=con.getInputStream();
          String encoding=con.getContentEncoding();
          if(null==encoding){
              encoding = "UTF-8";
          }
          final InputStreamReader inReader = new InputStreamReader (in, encoding);
          final BufferedReader buffReader = new BufferedReader (inReader);
          String line = null;

          while((line=bufReader.readLine())!=null){
              result.append(line);
          }
          bufReader.close();
          inReader.close();
          in.close();
      } else{
          System.out.println(status);
      }
    } catch(Exception e1){
      e1.printStackTrace();
    } US>finally
      if(con!=null){
        con.disconnect();
      }
    }
    return result.toString();
  }
}

java android line

2022-09-30 10:29

1 Answers

I think it would be better to organize how to communicate http on Android before implementing Oauth.

Summary of how to communicate with HTTP on Android - Qiita

If you would like to confirm communication in for now, wouldn't it be better to test it in a language that is easy to write about postman or other http communication and then re-implement your own communication system?


2022-09-30 10:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.