如何在Android中使用Twillio Api發送短信。 這是我的代碼。 我不知道的是如何設置http請求體。 當我使用CocoaRestClient(api測試工具)對其進行測試時,它運行良好。 請幫助我。如何在Android中使用Twilio Api發送短信
public void sendInviteSMS(String kToNumber) {
int random4Num = generateRequestCode();
...
String kTwilioSID = "...";
String kTwilioSecret = "...";
String kFromNumber = "...";
String message = String.format("%s has sent you a invite. To accept, enter the following code: %d.", AppUtil.sharedObject().userFirstName, random4Num);
String kMessage = message;
String urlString = String.format("https://%s:%[email protected]/2010-04-01/Accounts/%s/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID);
HashMap postData = new HashMap();
postData.put("From", kFromNumber);
postData.put("To", kToNumber);
postData.put("Body", kMessage);
// Validate user with the POST call
AsyncTask doPost = new TwilioPost(urlString) {
@Override
protected void onPostExecute(String result) {
Log.v("PHONE", result);
}
}.execute(postData);
}
...
public class TwilioPost extends AsyncTask<HashMap<String, String>, Void, String> {
private String remoteURL;
private static final String TAG = "Wayjer";
public TwilioPost(String remoteURL) {
this.remoteURL = remoteURL;
}
////////////////////////////////////////////
// Call "doPost" in the background thread
///////////////////////////////////////////
@Override
protected String doInBackground(HashMap<String, String>... hashMaps) {
try {
return doPost(hashMaps[0]);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
///////////////////////////////////////////////////////
// Override to convert result string to a JSONObject
//////////////////////////////////////////////////////
@Override
protected void onPostExecute(String result) {
try {
Log.v(TAG, result);
} catch (Exception e) {
Log.v(TAG, e.toString());
}
}
public String doPost(HashMap<String, String> postData) throws IOException {
URL url = new URL(remoteURL);
String response = "";
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
String postString = buildString(postData);
byte[] postBytes = postString.getBytes("UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(postBytes.length));
// Write parameter...
OutputStream outStream = connection.getOutputStream();
outStream.write(postBytes);
outStream.flush();
outStream.close();
connection.connect();
int resCode = connection.getResponseCode();
Log.v(TAG, "Response Message: " + connection.getResponseMessage());
if (resCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
response += line;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String buildString(HashMap<String, String> postData) throws UnsupportedEncodingException {
StringBuilder strBuilder = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : postData.entrySet()) {
try {
Log.v(TAG, "HTTPPOST ENTRY: " + entry.getKey() + " - " + entry.getValue());
if (first)
first = false;
else
strBuilder.append("&");
strBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
strBuilder.append("=");
strBuilder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
} catch (Exception e) {
}
}
return strBuilder.toString();
}
}
有人幫我嗎? :( –
[此頁上的解決方案](http://stackoverflow.com/a/38871516/5486128)可能會幫助您 – TarikW
看看這裏http://stackoverflow.com/questions/28109087/how-to-send -sms-using-twilio-in-my-android-application/41745246#41745246 – Sam