如何从Android应用程序发送HTTP请求到Heroku

时间:2015-07-21 19:17:30

标签: java android http heroku twilio

我有一个使用twilio sdk并由heroku服务器托管的Android应用程序。我正在尝试在我的应用程序中按一个按钮向heroku发送HTTP请求,以向Twilio发送REST API请求以更新我的twiml URL。我正在尝试发送HTTP请求的当前方式无效。我查看了所有可以找到的示例,但没有一个示例显示如何执行此功能。有人知道怎么做这个吗?提前谢谢。

这是我尝试将HTTP请求发送到heroku的代码

    holdButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {


            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://yourappnamehere.herokuapp.com/hello");

            try {
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity ht = response.getEntity();

                BufferedHttpEntity buf = new BufferedHttpEntity(ht);

                InputStream is = buf.getContent();

                BufferedReader r = new BufferedReader(new InputStreamReader(is));

                StringBuilder total = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    total.append(line);
                }

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //setting a toast to see if this is being initiated
            Toast.makeText(getBaseContext(), "why wont it work!", Toast.LENGTH_SHORT).show();
        }

        ;

    });

这是我更新的代码,包括凌空图书馆

            holdButton.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {
                    //setting up a request queue from Volley API
                    //RequestQueue mRequestQueue;

    // Instantiate the cache
                    Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

    // Set up the network to use HttpURLConnection as the HTTP client.
                    Network network = new BasicNetwork(new HurlStack());

    // Instantiate the RequestQueue with the cache and network.
                    mRequestQueue = new RequestQueue(cache, network);

    // Start the queue
                    mRequestQueue.start();

                    String url = "http://yourappnamehere.herokuapp.com/hello";

    // Formulate the request and handle the response.
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    // Do something with the response
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    // Handle error
                                }
                            });

                    // Add the request to the RequestQueue.
                    mRequestQueue.add(stringRequest);
                    Toast.makeText(getBaseContext(), "why wont it work!", Toast.LENGTH_SHORT).show();
   }

   ;

   });

1 个答案:

答案 0 :(得分:0)

我建议使用像Google Volley这样非常漂亮的库 https://developer.android.com/training/volley/index.html

HttpRequest已从API级别22弃用。最佳做法是避免使用它。请改用java.net.HttpUrlConnection。

但是,如果您仍想使用它,则上述代码需要在上述注释中提到的UI线程以外的线程上运行。