Я новичок в разработке для Android и пытаюсь отправить сообщение в службу уведомлений LINE, используя "Метод аутентификации -> POST https://notify -api.line.me / api / notify "( Документ API LINE Notify )
Вот мои коды, и я всегда получаю
Код статуса возврата: -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;
}
}
}
}