将cURL转换为Java

时间:2016-04-27 04:06:37

标签: java curl twilio

如何将其转换为纯java?

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/AC21bf240a783b67d2c09e5095554c2623/Messages.json' \
--data-urlencode 'To=+15133311772'  \
--data-urlencode 'From=+19375508241'  \
--data-urlencode 'Body=SYOOO ' \
-u AC21bf240a783b67d2c09e5095554c2623:{{ auth_token }}

注意:我尝试使用Twilio Java并收到此错误:

Process: edu.upenn.cis350.calorietracker, PID: 4505
java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:47)
                                                 at org.apache.http.impl.conn.PoolingClientConnectionManager.<init>(PoolingClientConnectionManager.java:93)
                                                 at com.twilio.sdk.TwilioClient.<init>(TwilioClient.java:147)
                                                 at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:23)
                                                 at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:19)

1 个答案:

答案 0 :(得分:2)

你有Twilio的JAVA API: https://www.twilio.com/docs/java/install

   import com.twilio.sdk.TwilioRestClient;
    import com.twilio.sdk.TwilioRestException;
    import com.twilio.sdk.resource.factory.MessageFactory;
    import com.twilio.sdk.resource.instance.Message;
    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;

    import java.util.ArrayList;
    import java.util.List;

    public class Example {

      // Find your Account Sid and Token at twilio.com/user/account
      public static final String ACCOUNT_SID = "AC32a3c49700934481addd5ce1659f04d2";
      public static final String AUTH_TOKEN = "{{ auth_token }}";

      public static void main(String[] args) throws TwilioRestException {
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

        // Build a filter for the MessageList
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Body", "Jenny please?! I love you <3"));
        params.add(new BasicNameValuePair("To", "+14159352345"));
        params.add(new BasicNameValuePair("From", "+14158141829"));

        MessageFactory messageFactory = client.getAccount().getMessageFactory();
        Message message = messageFactory.create(params);
        System.out.println(message.getSid());
      }
    }