错误:SMS API在Android应用中出现错误

时间:2018-10-28 09:56:21

标签: android android-studio sms

我正在使用SMS API,并且在Web上运行良好,但是在Android App中却出现错误,导致无法发送SMS。该API生成的响应为:

  

http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=bestfolio&password=12345678&sendername=AGPUBS&mobileno=913125603722&message=welcome

这是我的短信发送活动

    public class SmsSendActivity extends AsyncTask<String, Void, Void> {
    private Context context;
    private String mobile;
    //private String SMS;

    SmsSendActivity(Context context, String mobile, String SMS) {
        this.context = context;
        this.mobile = mobile;
        //  this.SMS = SMS;

    }

    @Override
    protected Void doInBackground(String... strings) {

        try {
            // Construct data
            String username = "username=" + "bestfolio";
            String password = "&password=" + "12345678";
            String sendername = "&sendername=" + "AGPUBS";
            String mobileno = "&mobileno=" + URLEncoder.encode(strings[0], "UTF-8");
            String message = "&message=" + URLEncoder.encode(strings[1], "UTF-8");
            String url = "http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?";

            // Send data
            String data = username + password + sendername + mobileno + message;
            Log.e("smsapi", url + data);
            HttpURLConnection conn = (HttpURLConnection) new URL(url + data).openConnection();

            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            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) {
                Log.e("SMS", "" + line);
            }
            rd.close();

            //return stringBuffer.toString();
        } catch (Exception e) {
            Log.e("Error is", "" + e);
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}

我在这里叫这堂课

new SmsSendActivity(getApplicationContext(), mobile, thanksMessage).execute(mobile, thanksMessage);

2 个答案:

答案 0 :(得分:1)

如果您收到NetworkOnMainThreadException,则表示您正在主线程上运行网络调用,这在Android中是不允许的。您需要使用AsyncTask或其他线程机制来在后台线程上运行网络调用,如here所示。

答案 1 :(得分:1)

public class MyAsyncTask extends AsyncTask{

private Context context; 

public MyAsyncTask(Context context) {  // can take other params if needed
    this.context = context;
}

// Add your AsyncTask methods and logic
//you can use your context variable in onPostExecute() to manipulate activity UI

} 然后在您的MainActivity中调用它

MyAsyncTask myTask = new MyAsyncTask(this);  //can pass other variables as needed
myTask.execute();