0
我想使用Twilio發送短信。我使用的是post中給出的代碼。但我得到的答覆代碼是201而不是200.答案<?xml version='1.0' encoding='UTF-8'?>
也很奇怪。代碼或證書有錯嗎?使用Twilio發送短信android
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
@Override
protected String doInBackground(String... params) {
try{
String twilioSID="XYZ";
String twilioSecret="ABC";
String urlStr = "https://"+twilioSID+":"+twilioSecret+"@api.twilio.com/2010-04-01/Accounts/"+twilioSID+"/SMS/Messages";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlStr);
String base64EncodedCredentials = "Basic "
+ Base64.encodeToString(
("XYZ" + ":" + "ABC").getBytes(),
Base64.NO_WRAP);
httppost.setHeader("Authorization", base64EncodedCredentials);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("From", "+12389234455"));
nameValuePairs.add(new BasicNameValuePair("To", "+918423664906"));
nameValuePairs.add(new BasicNameValuePair("Body", "Welcome to Twilio"));
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
System.out.println("sammy_response_code "+status);
HttpEntity entity = response.getEntity();
if(entity != null){
resp = EntityUtils.toString(entity);
}
}catch (Exception e){
System.out.println("sammy_Exception "+e);
}
return resp;
}
@Override
protected void onPostExecute(String result) {
//pd.dismiss();
System.out.println("sammy_response "+result);
}
}
https://www.twilio.com/blog/2016/05/how-to-send-an-sms-from-android.html –