Steps:
1. Watch the video for the designing part:
2. Declare the EditText and Button on MainActivity.java file as:
3. Initialize button and textfield inside the onCreate method as:
4. Create the listening event of button and write the following code inside that listening event as:
5. Create the thread and write the above code inside that thread or strict the thread and write the following code inside the onCreate method but outsize the listening event of button.
1. Watch the video for the designing part:
2. Declare the EditText and Button on MainActivity.java file as:
EditText _txtapi, _txtmess, _txtsender, _txtnum; Button _btnsend;
3. Initialize button and textfield inside the onCreate method as:
_txtapi = (EditText)findViewById(R.id.txtapi); _txtmess=(EditText)findViewById(R.id.txtmess); _txtsender=(EditText)findViewById(R.id.txtsender); _txtnum=(EditText)findViewById(R.id.txtphone); _btnsend=(Button)findViewById(R.id.btnsend);
4. Create the listening event of button and write the following code inside that listening event as:
_btnsend.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try {
// Construct data String apiKey = "apikey=" + _txtapi.getText().toString();
String message = "&message=" + _txtmess.getText().toString();
String sender = "&sender=" + _txtsender.getText().toString();
String numbers = "&numbers=" + _txtnum.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) {
//stringBuffer.append(line); Toast.makeText(getApplicationContext(), "the message is "+line, Toast.LENGTH_LONG).show();
}
rd.close();
//return stringBuffer.toString(); } catch (Exception e) {
//System.out.println("Error SMS "+e); Toast.makeText(getApplicationContext(), "the error message is"+e, Toast.LENGTH_LONG).show();
//return "Error "+e; }
}
});
5. Create the thread and write the above code inside that thread or strict the thread and write the following code inside the onCreate method but outsize the listening event of button.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
No comments:
Post a Comment