访问http资源之HttpUrlConnection get、post请求

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button doGet;
    Button doPost;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        doGet= (Button) findViewById(R.id.doGet);
        doPost= (Button) findViewById(R.id.doPost);
        doGet.setOnClickListener(this);
        doPost.setOnClickListener(this);
    }
    //按钮点点击监听事件
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.doGet:
                doGet();
                break;
            case R.id.doPost:
                doPost();
                break;
        }
    }
    //doPost方法实现
    private void doPost() {
        final String siteUrl="http://hn.jlpybwg.com/cs/";//请求地址
        //不能在主线程请求网络
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL(siteUrl);
                    HttpURLConnection httpUrlConnection= (HttpURLConnection) url.openConnection();
                    httpUrlConnection.setDoInput(true);
                    httpUrlConnection.setDoOutput(true);
                    httpUrlConnection.setRequestMethod("POST");
                    httpUrlConnection.setUseCaches(false);
                    httpUrlConnection.setRequestProperty("Accept-Charset","UTF-8");
                    httpUrlConnection.connect();
                    if(httpUrlConnection.getResponseCode()==200){
                        //获取输入流
                        InputStream inputStream=httpUrlConnection.getInputStream();
                        InputStreamReader ir=new InputStreamReader(inputStream);
                        BufferedReader bufferedReader=new BufferedReader(ir);
                        String readerLine="";
                        StringBuffer stringBuffer=new StringBuffer();//中转变量
                        stringBuffer.append("Post请求:");
                        while((readerLine=bufferedReader.readLine())!=null){
                            stringBuffer.append(readerLine);
                        }
                        Log.v("Log",stringBuffer.toString());
                        bufferedReader.close();
                        ir.close();
                        inputStream.close();
                        httpUrlConnection.disconnect();
                    }else{
                        Log.v("Log","请求失败");
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //doGet方法实现
    private void doGet() {
        final String siteUrl="http://hn.jlpybwg.com/cs/";//请求地址
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL(siteUrl);
                    HttpURLConnection httpUrlConnection= (HttpURLConnection) url.openConnection();
                    httpUrlConnection.connect();//连接服务器
                    //判断是否请求成功
                    if(httpUrlConnection.getResponseCode()==200){
                        //获取输入流
                        InputStream inputStream=httpUrlConnection.getInputStream();
                        InputStreamReader ir=new InputStreamReader(inputStream);
                        BufferedReader bufferedReader=new BufferedReader(ir);
                        String readerLine="";
                        StringBuffer stringBuffer=new StringBuffer();//中转变量

                        while((readerLine=bufferedReader.readLine())!=null){
                            stringBuffer.append(readerLine);
                        }
                        Log.v("Log",stringBuffer.toString());
                        bufferedReader.close();
                        ir.close();
                        inputStream.close();
                        httpUrlConnection.disconnect();
                    }else{
                        Log.v("Log","请求失败");
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}

activityx_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.ccyumo.myservice.MainActivity">

    <Button
        android:id="@+id/doPost"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#cccccc"
        android:gravity="center"
        android:text="doPost" />

    <Button
        android:id="@+id/doGet"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:background="#cccccc"
        android:gravity="center"
        android:text="doGet" />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_44174689/article/details/85159879