我的朋友需要有关她的论文的帮助。她正在制作一个使用短信网关的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
答案 0 :(得分:0)
正如@CommonsWare建议的那样,使用更现代的HTTP客户端API可以正常工作(在这种情况下,使用的是OkHttp)。