发送短信时文件IO异常

FileIO exception when sending a sms message

我朋友的论文需要帮助。她正在制作一个使用短信网关的 Android 应用程序(她为此使用 sms gateway by wavecell)。但是当她提交一条短信时,它给出了一个关于 FileIO 异常的错误。我已经尝试检查并给出她的建议,但我没有太多使用短信网关的经验,所以我不确定我是否遗漏了什么。

她说她的服务余额还够用,还没有过期。她还尝试生成一个新的访问令牌并将所需的输入更改为硬编码值,但它仍然不起作用。

代码如下:

package com.example.johnica.mysmsgateway;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.StrictMode;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.Response;

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

public class MainActivity extends AppCompatActivity {

    Button send;
    EditText number, message1;

    final int SEND_SMS_PERMISSION_REQUEST_CODE = 1;

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

        send=findViewById(R.id.buttonSend);
        message1=findViewById(R.id.inputMessage);
        number=findViewById(R.id.inputNumber);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // Construct data

                    String text = "&text=" + message1.getText().toString();
                    String source = "&source=" + "Take Care";
                    String destination = "&destination=" + number.getText().toString();

                    HttpURLConnection conn = (HttpURLConnection) new URL("https://api.wavecell.com/sms/v1/{sub account id here}/single").openConnection();

                    String data = destination + text + source;

                    conn.setDoOutput(true);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Authorization", "Bearer {used a token here}");
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setRequestProperty("Content-Length", Integer.toString(data.length()));
                    conn.getOutputStream().write(data.getBytes("UTF-8"));
                    final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    final StringBuffer stringBuffer = new StringBuffer();
                    String line;
                    while ((line = rd.readLine()) != null) {
                        Toast.makeText(MainActivity.this,line.toString(), Toast.LENGTH_SHORT).show();
                    }
                    rd.close();

                } catch (Exception e) {
                    Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        StrictMode.ThreadPolicy st= new StrictMode.ThreadPolicy.Builder().build();
        StrictMode.setThreadPolicy(st);

    }
}

按下发送按钮时,堆栈跟踪生成 JAVA IO file not found exception url https://developer.wavecell.com/v1/sms-api

正如@CommonsWare 所建议的,使用更现代的 HTTP 客户端 API 有效(在本例中,使用了 OkHttp)。