post方式提交数据

package com.example.getmenthod;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MyActivity extends Activity {
    private EditText ed1;
    private EditText ed2;
    private Button btn;
    private Handler handler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed1 = (EditText) findViewById(R.id.et1);
        ed2 = (EditText) findViewById(R.id.et2);
        btn = (Button) findViewById(R.id.btn);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                String resu = (String) msg.obj;
                Toast.makeText(MyActivity.this,resu,Toast.LENGTH_SHORT).show();
            }
        };

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = ed1.getText().toString();
                String password = ed2.getText().toString();
                String path = "http://192.168.21.1:8080/ok/servlet/Login";
                getMethod(path,username,password);
            }
        });

    }
    private void getMethod(final String path,final String username,final String password){
       //线程里访问网络
        Thread thread = new Thread(){
           @Override
           public void run() {
               try {
                   URL url = new URL(path);
                   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                   conn.setRequestMethod("POST");
                   //添加表单信息
                   StringBuffer sb = new StringBuffer();
                   sb.append("username=");
                   sb.append(username);
                   sb.append("&password=");
                   sb.append(password);
                   //转换成字节数组
                   byte[] data = sb.toString().getBytes();

                   conn.setReadTimeout(5000);
                   conn.setConnectTimeout(5000);
                   //设置提交的属性
                   conn.setRequestProperty("Content-Type",
                           "application/x-www-form-urlencoded");

                   conn.setRequestProperty("Content-Length",
                           String.valueOf(data.length));
                   //用输出流发送给服务器
                   OutputStream outputStream = conn.getOutputStream();
                   outputStream.write(data);
                   if(conn.getResponseCode()==200){
                       //获得输入流
                       InputStream in = conn.getInputStream();
                       String result = getDataFromIn(in);
                       //消息队列发送消息
                       Message message = handler.obtainMessage();
                       message.obj = result;
                       handler.sendMessage(message);

                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       };
        thread.start();


    }
    private static String getDataFromIn(InputStream in){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while ((len = in.read(b))!=-1){
                out.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(out.toByteArray());
    }


}
原文地址:https://www.cnblogs.com/84126858jmz/p/4945599.html