user12556987
user12556987

Reputation:

How to send text messages in android using textlocal

I want to send messages using textlocal.I register on the site,created a new Api Key and followed a youtube tutorial but Iam not getting the message.Instead Iam getting a toast of success only. Iam uploading the code please any can help me out that whats Iam missing here it not sending the text to device only giving me toast of success

public class MainActivity extends AppCompatActivity {

    private EditText editTextTo, editTextMessage;
    RelativeLayout activity_main;
    Button button;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity_main = (RelativeLayout) findViewById(R.id.activity_main);
        editTextTo = (EditText) findViewById(R.id.editTextTo);
        editTextMessage = (EditText) findViewById(R.id.editTextMessage);
        button = (Button) findViewById(R.id.btn);
        requestQueue = Volley.newRequestQueue(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // Construct data
                    String apiKey = "apikey=" + "<SECRET-API-KEY>";
                    String message = "&message=" + editTextMessage.getText().toString();
                    String sender = "&sender=" + "<SENDER-NAME>";
                    String numbers = "&numbers=" + editTextTo.getText().toString();

                    // Send data
                    HttpURLConnection conn = (HttpURLConnection) new URL("https://api.txtlocal.com/send/?").openConnection();
                    String data = apiKey + numbers + message + sender;
                    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) {
                        Toast.makeText(MainActivity.this, line.toString(), Toast.LENGTH_LONG).show();
                    }
                    rd.close();


                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        StrictMode.ThreadPolicy st = new StrictMode.ThreadPolicy.Builder().build();
        StrictMode.setThreadPolicy(st);
    }

Upvotes: 0

Views: 1972

Answers (2)

user13933452
user13933452

Reputation: 1

TextLocal in nodejs use can run npm i node-fetch and npm i textlocal. create Post Api and paste this code. const response = await fetch(https://api.textlocal.in/send/?apiKey=yourapikey&sender=yoursender&numbers=${req.body.mobile}&message=your message..., { method: 'GET' }); const json1=await response.json(); console.log('textlocal',json1);

Upvotes: -1

Umair Iqbal
Umair Iqbal

Reputation: 2069

Iam using sendpk their service and packages are good.Simply register to their site you will get 5 free messages after testing you can buy a packages.They are offering variety of options.After registering you can get the Api for java,php etc.Just put it in you code and boom you are ready to go. Iam posting a code I used in for testing you can ammend as per your needs

XML :

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="To"
            android:inputType="phone"
            android:id="@+id/editTextTo"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Message"
            android:id="@+id/editTextMessage"/>
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"/>
    </LinearLayout>

JAVA :

public class MainActivity extends AppCompatActivity {
    private EditText editTextTo, editTextMessage;
    RelativeLayout activity_main;
    Button button;

    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity_main = (RelativeLayout) findViewById(R.id.activity_main);
        editTextTo = (EditText) findViewById(R.id.editTextTo);
        editTextMessage = (EditText) findViewById(R.id.editTextMessage);
        button = (Button) findViewById(R.id.btn);
        requestQueue = Volley.newRequestQueue(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                if (editTextTo.getText().toString().isEmpty()) {
                    Snack("Number is empty");
                } else if (editTextTo.getText().toString().isEmpty()) {
                    Snack("Check the number");
                } else if (editTextMessage.getText().toString().isEmpty()) {
                    Snack("Message is empty");
                } else {
                    SendSms(editTextTo.getText().toString(), editTextMessage.getText().toString());
                }
            }
        });
    }


    public void Snack(String message) {
        Snackbar.make(activity_main, message, Snackbar.LENGTH_LONG).setAction("Action", null).show();
    }


    public void SendSms(final String to, final String message) {

        StringRequest menuRequest = new StringRequest(Request.Method.POST, "https://sendpk.com/api/sms.php?",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonResponse = new JSONObject(response).getJSONArray("check");

                            int i = jsonResponse.length();
                            for (int j = 0; j < i; j++) {
                                JSONObject jsonChildNode = jsonResponse.getJSONObject(j);
                                Snack(jsonChildNode.optString("sms").toString());

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.e("Json", e.getMessage());
                            Snack("Json " + e.getMessage());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        Snack("Volley " + error.getMessage());
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("username", "username");
                params.put("password", "password");
                params.put("sender", "name with which you want to send message(whihc appear instead of number and you can also have number over here like 8383)");
                params.put("mobile", "mobile number which will recieve);
                params.put("message", message);
                return params;
            }
        };

    }

}

Upvotes: 0

Related Questions