gpt4 book ai didi

android - 错误 : SMS API getting Error in Android App

转载 作者:行者123 更新时间:2023-11-29 18:41:12 24 4
gpt4 key购买 nike

我正在开发一个 SMS API,它在 Web 上运行良好,但在 Android 应用程序中出现无法发送 SMS 的错误。从该 API 生成的响应是:

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

这是我的短信发送 Activity

    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);

最佳答案

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();

关于android - 错误 : SMS API getting Error in Android App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53030259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com