【发布时间】:2015-08-20 10:57:21
【问题描述】:
我想知道下面的代码有什么问题以及为什么它不发送短信,当我调试时没有发现错误。请问我可以得到帮助吗?
public class LaunchSMS extends Activity implements OnClickListener {
private TextView phone;
private TextView phone2;
private EditText TFmsg;
private TextView TVfrom;
Button btnsend;
static final String username = "xxxxxxx";
static final String password = "xxxxxxx";
static String url = "http://www.esmsafrica.com/components/com_spc/smsapi.php";
static final String charset = "UTF-8";
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
TFmsg = (EditText) findViewById(R.id.TFmsg);
phone = (TextView) findViewById(R.id.phone);
btnsend = (Button) findViewById(R.id.btnsend);
Button mBtnContacts = (Button) findViewById(R.id.mBtnContacts);
Button btnLogout = (Button) findViewById(R.id.btnlogout);
mBtnContacts.setOnClickListener(this);
btnsend.setOnClickListener(this);
btnLogout.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE},
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
showSelectedNumber(number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(String number) {
phone.setText(number);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.mBtnContacts) {
// user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
if (v.getId() == R.id.btnsend) {
String reciever = phone.getText().toString();
String message = TFmsg.getText().toString();
if (reciever.length() > 0 && message.length() > 0)
try {
sendSMS(reciever, message);
} catch (Exception e) {
e.printStackTrace();
}
else
Toast.makeText(getBaseContext(),
"Please enter both reciever number and message.",
Toast.LENGTH_SHORT).show();
}
if (v.getId() == R.id.btnlogout) {
logoutUser();
}
}
private void logoutUser() {
Intent intent = new Intent(LaunchSMS.this, Sendsms.class);
startActivity(intent);
finish();
}
public static void sendSMS(String reciever, String message) throws Exception {
System.out.println("Welcome to eSMS");
//To establish the connection and perform the post request
URLConnection connection = new URL(url + "?" + buildRequestString(reciever,message)).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
//This automatically fires the request and we can use it to determine the response status
InputStream response = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(response));
//System.out.println(br);
System.out.println(br.readLine());
}
private static String buildRequestString(String reciever, String message) throws UnsupportedEncodingException {
String [] params = new String [5];
params[0] = username;
params[1] = password;
params[2] = message;
params[3] = reciever;
params[4] = "esmsafrica";
String query = String.format("uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s",
URLEncoder.encode(params[0],charset),
URLEncoder.encode(params[1],charset),
URLEncoder.encode(params[2],charset),
URLEncoder.encode(params[3],charset),
URLEncoder.encode(params[4],charset)
);
return query;
}
public static void main(String [] args) throws Exception {
System.out.println("enter Mobile No:");
Scanner scanIn = new Scanner(System.in);
String testPhoneNo = scanIn.nextLine();
scanIn.close();
String testMessage = "Sending Messages";
sendSMS(testPhoneNo, testMessage);
}
}
【问题讨论】:
标签: java android sms sms-gateway