簡體   English   中英

發送短信時發生FileIO異常

[英]FileIO exception when sending a sms message

我的朋友需要她的論文幫助。 她正在制作一個使用短信網關的Android應用程序(她正在使用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)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM