我想使用textlocal发送消息。我在网站上注册,创建了一个新的Api Key,然后按照youtube教程进行操作,但Iam没有收到消息。相反,Iam仅获得了成功。Iam上传代码,任何人都可以帮助我,我在这里遗漏了什么,它没有将文本发送到设备,只是给了我成功的敬酒
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表示他们的服务和套餐很好。只需注册到他们的网站,您就可以在购买测试包后获得5条免费消息。他们提供了多种选择。注册后,您可以获得Api java,php等。只需将其放入代码中,即可开始使用。Iam发布了我用于测试的代码,您可以根据需要进行修改
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;
}
};
}
}