android 与服务器get或post交互数据

时间:2015-05-20 12:53:59   收藏:0   阅读:233
package mydemo.mycom.demo2.service;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import mydemo.mycom.demo2.utils.StreamTools;

/**
 * Created by Administrator on 2015/5/20.
 */
public class NetService {

    public static String loginByGet(String username,String password)
    {
        try
        {
            String path = "http://192.168.1.110:1010/UserInfo/Login?username="+username+"&password="+password;
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            int code = conn.getResponseCode();
            if(code==200)
            {
                //请求成功
                InputStream is = conn.getInputStream();
                String result = StreamTools.readInputStream(is);
                return result;
            }
            else
            {
                //请求失败
                return null;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String loginByPost(String username,String password)
    {
        try
        {
            String path = "http://192.168.1.110:1010/UserInfo/Login";
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            String data = "username="+username+"&password="+password;

            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",data.length()+"");

            //post 的方式 实际上是浏览器把数据 写给了服务器
            conn.setDoInput(true);
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            int code = conn.getResponseCode();
            if(code==200)
            {
                //请求成功
                InputStream is = conn.getInputStream();
                String result = StreamTools.readInputStream(is);
                return result;
            }
            else
            {
                //请求失败
                return null;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

2.StreamTools

package mydemo.mycom.demo2.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTools {
    public static String readInputStream(InputStream is)
    {
        try
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len =0;
            byte[] buffer = new byte[1024];
            while((len=is.read(buffer))!=-1)
            {
                baos.write(buffer,0,len);
            }
            is.close();
            baos.close();

            byte[] result = baos.toByteArray();
            String temp = new String(result);
            return temp;
        }catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }

    }

}

 

原文:http://www.cnblogs.com/zoro-zero/p/4516842.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!