android HttpClient将数据提交到服务器

1、HttpClient 使用方式

复制代码
public static String loginByClientGet(String username,String password)
    {
        try {
            
            //打开浏览器
            HttpClient client = new DefaultHttpClient();
            
            //输入地址(url)
            String url = "http://192.168.1.100:8088/Login.ashx?username="+username+"&password="+password;
            HttpGet httpGet = new HttpGet(url);
            
            //按回车(发请求http get请求)
            HttpResponse response = client.execute(httpGet);
            
            //得到相应码
            int code = response.getStatusLine().getStatusCode();
            
            if(code==200)
            {    
                //得到相应实体
                HttpEntity entity = response.getEntity();
                //得到相应内容
                InputStream is = entity.getContent();
                return StreamUtil.readInputStream(is);
            }
            else
            {
                return null;
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    public static String loginByClientPost(String username,String password)
    {
        
        try
        {
            //打开浏览器
            HttpClient client = new DefaultHttpClient();
        
            //输入地址(输入url) 
            String url = "http://192.168.1.100:8088/Login.ashx";
            //使用post请求
            HttpPost httpPost = new HttpPost(url);
            
            //输入指定提交的数据实体
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            
            //得到相应
            HttpResponse response = client.execute(httpPost);
            
            //得到响应码
            int code = response.getStatusLine().getStatusCode();
            if(code==200)
            {
                //得到相应内容
                InputStream is = response.getEntity().getContent();
                return StreamUtil.readInputStream(is);
            }
            else
            {
                return null;
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
复制代码

2、InputStream转为String方法

复制代码
package com.example.getserverdata.utils;

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

public class StreamUtil {
    
    public static String readInputStream(InputStream is)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        byte[] data = new byte[1024];  
        int len = 0;  
        try {
            while((len = is.read(data))!=-1)  
                baos.write(data, 0, len);
            is.close();
            baos.close();
            return new String(baos.toByteArray());
            
        } catch (Exception e) {
        
            e.printStackTrace();
        }  
          
        return null;
    }
}
复制代码

 

posted @   大空白纸  阅读(297)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示