android app与服务器交互
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android app与服务器交互,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4343字,纯文字阅读大概需要7分钟。
内容图文
![android app与服务器交互](/upload/InfoBanner/zyjiaocheng/1090/6f4d07acd4f441f6bc337eb95e3b3b9d.jpg)
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 { // 使用URL的Get 方式请求服务器 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 { //请求失败returnnull; } } catch (Exception e) { e.printStackTrace(); } returnnull; } //使用URL的Post 方式请求服务器publicstatic 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 { //请求失败returnnull; } } catch (Exception e) { e.printStackTrace(); } returnnull; } //使用HttpClient的Get 方式请求服务器 (HttpClient 过时)publicstatic 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 { //请求失败returnnull; } } catch (Exception e) { e.printStackTrace(); } returnnull; } //使用HttpClient的Post 方式请求服务器 (HttpClient 过时)publicstatic 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 { //请求失败returnnull; } } catch (Exception e) { e.printStackTrace(); } returnnull; } }
//将InputStream 转为 String
package mydemo.mycom.demo2.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTools { // InputStream 转为 String public static String readInputStream(InputStream is) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len =0; byte[] buffer = newbyte[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(); returnnull; } } }
原文:http://www.cnblogs.com/zoro-zero/p/4682085.html
内容总结
以上是互联网集市为您收集整理的android app与服务器交互全部内容,希望文章能够帮你解决android app与服务器交互所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。