如何在ANDROID中集成TWILIO短代码api?

时间:2014-11-07 12:44:31

标签: android twilio shortcode

我想在我的Android应用程序中使用twilio短代码。 我收到这个错误。是否有任何解决方案。我坚持这个问题。

 11-07 18:04:54.442: E/AndroidRuntime(474): FATAL EXCEPTION: Thread-10
11-07 18:04:54.442: E/AndroidRuntime(474): java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>
11-07 18:04:54.442: E/AndroidRuntime(474):  at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:47)
11-07 18:04:54.442: E/AndroidRuntime(474):  at org.apache.http.impl.conn.PoolingClientConnectionManager.<init>(PoolingClientConnectionManager.java:93)
11-07 18:04:54.442: E/AndroidRuntime(474):  at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:147)
11-07 18:04:54.442: E/AndroidRuntime(474):  at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:117)
11-07 18:04:54.442: E/AndroidRuntime(474):  at com.example.twil.MainActivity$1.run(MainActivity.java:51)
11-07 18:04:54.442: E/AndroidRuntime(474):  at java.lang.Thread.run(Thread.java:1019)

我的代码如下:

public static final String ACCOUNT_SID = "XXXXXXXXXXXX";
public static final String AUTH_TOKEN = "XXXXXXXXXXXXX";
public String url = "https://api.twilio.com/2010-04-01/Accounts/";

    private void getData() {

    new Thread(new Runnable() {

        @Override
        public void run() {

             TwilioRestClient cl = new TwilioRestClient(ACCOUNT_SID,AUTH_TOKEN);

            String urls = url + ACCOUNT_SID + "/Messages.json";

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Body", "Hello"));
            params.add(new BasicNameValuePair("To", "+919638038525"));
            params.add(new BasicNameValuePair("From", "+12027938259"));
            //params.add(new BasicNameValuePair("", ACCOUNT_SID+":"+AUTH_TOKEN));

            String paramString = URLEncodedUtils.format(params, "utf-8");
            //String newurls = urls + paramString + "/" + ACCOUNT_SID + ":" + AUTH_TOKEN;
            String newurls = urls + paramString;
            Log.e("cat URL", newurls);
            String json = post(newurls);
            Log.e("json", json);
        }
    }).start();
}


private String post(String url) {
    InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";
        JSONObject jsonObject = new JSONObject();
        // jsonObject.accumulate("X-Mashape-Key",
        // "zgLkefJL03msh7C1P8bTXYXWykrLp1EFzJJjsnzcFhkwB63em9");
        json = jsonObject.toString();
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the
        // content
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // 10. convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return result;
}

private static String convertInputStreamToString(InputStream inputStream)
        throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}

使用的库:twilio-java-sdk-3.4.6-jar-with-dependencies.jar

1 个答案:

答案 0 :(得分:0)

你不应该从Android发送消息,提供的SDK是用于JAVA的,最好在后端服务器上使用,只需触发服务器发送消息