如何使用 textlocal 在 android 中发送短信

How to send text messages in android using textlocal

我想使用 textlocal 发送消息。我在网站上注册,创建了一个新的 Api 密钥并遵循了 youtube 教程,但我没有得到 message.Instead 我只为成功干杯。 我正在上传代码,任何人都可以帮助我解决我在这里遗漏的问题,它不会将文本发送到设备,只会让我为成功干杯

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

我正在使用 sendpk 他们的服务和包裹是 good.Simply 注册到他们的网站你将在测试后获得 5 条免费消息你可以购买 packages.They提供各种 options.After 注册,您可以获得 java、php etc.Just 的 Api 将其放入您的代码中,您就可以开始了。 我正在发布我用于测试的代码,您可以根据需要进行修改

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

    }

}

Nodejs 中的 TextLocal 使用 运行 npm i node-fetch 和 npm i textlocal。创建 Post Api 并粘贴此代码。 const response = await fetch(https://api.textlocal.in/send/?apiKey=yourapikey&sender=yoursender&numbers=${req.body.mobile}&message=your 消息..., { 方法: 'GET' }); const json1=await response.json(); console.log('textlocal',json1);