簡體   English   中英

如何從Android應用程序向Heroku發送HTTP請求

[英]How to send HTTP request from android app to Heroku

我有一個使用twilio sdk並由heroku服務器托管的android應用。 我試圖按我的應用程序中的按鈕以將HTTP請求發送到heroku,以將REST API請求發送到Twilio以更新我的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();
   }

   ;

   });

我建議使用像Google Volley這樣的庫,它非常漂亮https://developer.android.com/training/volley/index.html

從API級別22開始不推薦使用HttpRequest。最好的做法是避免使用它。 請改用java.net.HttpUrlConnection。

但是,如果仍要使用它,則上面的代碼需要在上面注釋中提到的UI線程之外的其他線程上運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM