android get或post及HttpClient与服务器数据交互
1、Service
package mydemo.mycom.demo2.service; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; 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; } public static String loginByClientGet(String username, String password) { try { HttpClient client = new DefaultHttpClient(); String path = "http://192.168.1.110:1010/UserInfo/Login?username=" + username + "&password=" + password; HttpGet httpget = new HttpGet(path); HttpResponse response = client.execute(httpget); int code = response.getStatusLine().getStatusCode(); if (code == 200) { //请求成功 InputStream is = response.getEntity().getContent(); String result = StreamTools.readInputStream(is); return result; } else { //请求失败 return null; } } catch (Exception e) { e.printStackTrace(); } return null; } public static String loginByClientPost(String username, String password) { try { HttpClient client = new DefaultHttpClient(); String path = "http://192.168.1.110:1010/UserInfo/Login"; HttpPost httppost = new HttpPost(path); //指定要提交的数据实体 List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("username", username)); list.add(new BasicNameValuePair("password", password)); httppost.setEntity(new UrlEncodedFormEntity(list, "UTF_8")); HttpResponse response = client.execute(httppost); int code = response.getStatusLine().getStatusCode(); if (code == 200) { //请求成功 InputStream is = response.getEntity().getContent(); 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; } } }
分类:
Android技术
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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