Error: SMS API getting Error in Android App
Error: SMS API getting Error in Android App
我正在处理 SMS API,它在 Web 上运行良好,但在 Android 应用程序中出现错误,无法发送 SMS。 API 生成的响应是:
这是我发送的短信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);
}
}
这里我叫这个class
new SmsSendActivity(getApplicationContext(), mobile, thanksMessage).execute(mobile, thanksMessage);
如果您收到 NetworkOnMainThreadException,则意味着您正在 运行 在主线程上进行网络调用,这在 Android 中是不允许的。您需要使用 AsyncTask 或其他线程机制 运行 后台线程上的网络调用,如图 here.
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();
我正在处理 SMS API,它在 Web 上运行良好,但在 Android 应用程序中出现错误,无法发送 SMS。 API 生成的响应是:
这是我发送的短信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);
}
}
这里我叫这个class
new SmsSendActivity(getApplicationContext(), mobile, thanksMessage).execute(mobile, thanksMessage);
如果您收到 NetworkOnMainThreadException,则意味着您正在 运行 在主线程上进行网络调用,这在 Android 中是不允许的。您需要使用 AsyncTask 或其他线程机制 运行 后台线程上的网络调用,如图 here.
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();