我正在尝试使用Twilio发送短信。我正在使用此post中给出的代码。但我得到的响应代码为201而不是200.而且响应<?xml version='1.0' encoding='UTF-8'?>
很奇怪。代码或凭证有问题吗?
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);
}
}
答案 0 :(得分:0)
请检查以下所有内容:
201回复
答案 1 :(得分:0)
当我在网址后面添加“.json”时,我收到了回复。现在即使响应代码是201,响应也会响起。