アンドロイドのHttpURLConnectionを使用してHTTPリクエストを送信します

ここでは、我々はすでに正式勧告のHttpURLConnectionの使用法を学びます。

HttpURLConnectionの最初のインスタンスを取得するには

私達はちょうど新しいURLオブジェクトを必要とし、先のURLを渡し、その後、OpenConnectionメソッド()メソッドを呼び出します。
URL url=new URL("http://www.baidu.com");
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();

GETとPOST:HttpURLConnectionの例を取得した後、我々はHTTPリクエストメソッドが使用されている設定することができ、2つの一般的な方法があります。GETは、サーバ内部にデータを提出することが、サーバー、POSTの願いからデータを取得したいです。

connection.setRequestMethod("GET");

残りは、私たちが読むために、そのようなリンクのタイムアウトを設定すると、タイムアウト(ミリ秒)をカスタマイズするいくつかの自由を行い、サーバ・メッセージ・ヘッダーの一部を得ることを期待することができます。以下のような:

connection.setConnectTimeout(8000);
        connection.setReadTimeout(8000);

getInputStream()メソッドを呼び出すした後に、この接続はHTTP閉鎖されるに示されるように残るが、次に、入力ストリームを読み取る方法)(disconnec呼び出すことであることを、サーバから返された入力ストリームを得るために使用することができます。

InputStream in=connection.getInputStream();
        connection.disconnect();

すべての部分が完成したプレゼンテーションされた後、私たちは、HttpURLConnectionの下の使用状況を体験する例を使用します。
XMLレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/BT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Request"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/response_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
</LinearLayout>

活動セクション:

Button sendRequest=(Button)findViewById(R.id.BT);
        responseText=(TextView) findViewById(R.id.response_text);
        sendRequest.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                    HttpURLConnection connection=null;
                        BufferedReader reader=null;
                     try{
                     URL url=new URL("http://www.sina.com");
                            connection=(HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");
                            connection.setReadTimeout(8000);
                            connection.setReadTimeout(8000);

                            InputStream in=connection.getInputStream();

                            reader=new BufferedReader(new InputStreamReader(in));
                            StringBuilder response=new StringBuilder();
                            String line;
                            while ((line=reader.readLine())!=null){
                                response.append(line);

                            }
                            showResponse(responseData);
                        }catch (Exception e){
                            e.printStackTrace();

                        }finally {

                            if (reader!=null){
                                try {
                                    reader.close();
                                }catch (IOException e){
                                    e.printStackTrace();
                                }

                            }
                            if(connection!=null){
                                connection.disconnect();
                            }
                        }


                    }
                }).start();

            }
            private void showResponse(final String response){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        responseText.setText(response);
                    }
                });

            }
        });

仕上げにHTTPリクエストの内容を送信するが、OkHttp使用方法を読んだ後、私たちはありません再びHttpURLConnectionのアップのHttpURLConnectionを入れて、私たちはそう。次は私が使用する方法です最後にOkHttp書いたブログ。
今、彼はまだ白で、郭林は、学生が学ぶのAndroidに来てほしい場合は、私たちが一緒に学ぶことができ、再びノックの教師自身の理解を置きます。

公開された37元の記事 ウォン称賛10 ビュー10000 +

おすすめ

転載: blog.csdn.net/OneLinee/article/details/78369129