Afaque
Afaque

Reputation: 23

Error: SMS API getting Error in Android App

I am working on an SMS API and it is working fine on Web but it is getting me error in Android App that SMS cannot be sent. The response generated from that API is:

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

Here is my SMS Send 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);
    }
}

Here i am calling this class

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

Upvotes: 1

Views: 75

Answers (2)

Jawad Malik
Jawad Malik

Reputation: 618

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

} then call it in your MainActivity

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

Upvotes: 1

Ravi Patel
Ravi Patel

Reputation: 2191

If you are getting NetworkOnMainThreadException then it means you are running network call on main thread which is not allowed in Android. You need to use AsyncTask or another threading mechanism to run the network call on a background thread as shown here.

Upvotes: 1

Related Questions