【问题标题】:Error: SMS API getting Error in Android App错误:SMS API 在 Android 应用程序中出现错误
【发布时间】:2018-10-31 16:17:15
【问题描述】:

我正在开发 SMS API,它在 Web 上运行良好,但在 Android 应用程序中出现无法发送 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);

【问题讨论】:

  • 你得到了哪个例外?
  • 没有任何异常;我从服务器收到此响应:发送消息时发生错误
  • 尝试设置 conn.setRequestMethod("POST");获取
  • 不,是get方法
  • 当我将它与 AppCompatActivity 一起使用时它工作正常,但现在我想直接发送短信,现在它让我出错

标签: android android-studio sms


【解决方案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();

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 您应该使用内部类而不是在您的 Activity 类中扩展它。看this的答案供参考。
    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多