无法将消息发送到LINE Notify(使用HttpsURLConnection POST)

问题描述 投票:1回答:1

我是Android开发的新手,我正尝试通过使用“身份验证方法-> POST https://notify-api.line.me/api/notify”(LINE Notify API Document)将消息发送到LINE通知服务

这是我的密码,我总是会得到

返回状态码:-1

。请帮助我找出问题出在哪里?

LINENotify.java] >>

package com.example.test3;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class LINENotify {

    private static final String strEndpoint = "https://notify-api.line.me/api/notify";
    private static final String tokenKEY = "ldO3dTIAjT5OpjWbeZAQtnXhq23poCXcEAvKoTwJHhn";

    public int sendMSG( String message ) {

        int statusCode;
        statusCode = -1;

        try {
            URL url = new URL( strEndpoint );
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

            connection.setRequestMethod( "POST" );
            connection.addRequestProperty("Authorization",  "Bearer " + tokenKEY);
            connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            connection.setConnectTimeout(15*1000);
            connection.setDoOutput( true );
            connection.setUseCaches( false );
            connection.connect();

            connection.setReadTimeout(15*1000);
            connection.setDoInput( true );

            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( os, "UTF-8") );

            writer.write( "message=" + message );
            writer.flush();
            writer.close();

            os.close();

            statusCode = connection.getResponseCode();

            if ( statusCode == 200 ) {
                //
            } else {
                throw new Exception( "Error:(StatusCode)" + statusCode + ", " + connection.getResponseMessage() );
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return statusCode;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

</manifest>

MainActivity.java

package com.example.test3;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText msg;
    Button send;

    LINENotify lineNotify;

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

        msg = (EditText) findViewById(R.id.editText);
        send = (Button) findViewById(R.id.button);

        lineNotify = new LINENotify();

        send.setOnClickListener(this);
    }// onCreate

    @Override
    public void onClick(View v) {
        switch( v.getId() ) {
            case R.id.button: {
                int statusCode = lineNotify.sendMSG( msg.getText().toString() );
                Toast.makeText(this, "Return status code: " + statusCode, Toast.LENGTH_LONG).show();
                break;
            }
        }
    }
}

我是Android开发的新手,我正在尝试通过使用“身份验证方法-> POST https://notify-api.line.me/api/notify”将消息发送到LINE通知服务(LINE通知API文档)...

java android http-post httpurlconnection httpsurlconnection
1个回答
0
投票

我得到了答案!感谢Google和张贴他们的经验和笔记的人。

© www.soinside.com 2019 - 2024. All rights reserved.