My friend needs help with her thesis. She's making an Android app that uses a sms gateway (she's using a sms gateway by wavecell for this). But when she submits a text message, it gives an error about a FileIO exception. I already tried checking and giving her suggestions but I don't have much experience with using a sms gateway so I'm not sure if I missed something.
She says her balance for the service is still enough and hasn't expired yet. She's also tried generating a new access token and changing the inputs required to hardcoded values but it's still not working.
Here's the code:
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);
}
}
When the send button is pressed, the stacktrace produces JAVA IO file not found exception url https://developer.wavecell.com/v1/sms-api
As @CommonsWare suggested, working with a more modern HTTP client API worked (in this case, OkHttp was used).