访问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




    

你可能感兴趣的:(android)