标签:http协议
package com.example.util; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.util.Log; public class HttpUtils { public HttpUtils() { } /** * 根据手机号从服务器获取相关信息 * 从服务器上获取指定的内容-POST * */ public static String getDataFromServer(String svrUrl, String ParamStr) { Log.v("geek", "url = " + svrUrl); Log.v("geek", "ParamStr = " + ParamStr); try { // 转成指定格式 byte[] requestData = ParamStr.getBytes("UTF-8"); HttpURLConnection conn = null; DataOutputStream outStream = null; String MULTIPART_FORM_DATA = "multipart/form-data"; // 构造一个post请求的http头 URL url = new URL(svrUrl); // 服务器地址 conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // 允许输入 conn.setDoOutput(true); // 允许输出 conn.setUseCaches(false); // 不使用caches conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA); conn.setRequestProperty("Content-Length", Long.toString(requestData.length)); // 请求参数内容, 获取输出到网络的连接流对象 outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(requestData, 0, requestData.length); outStream.flush(); outStream.close(); ByteArrayOutputStream outStream2 = new ByteArrayOutputStream(); int cah = conn.getResponseCode(); if (cah != 200) { Log.v("zd", "服务器响应错误代码:" + cah); return "0"; }else if(cah == 200){ Log.v("zd", "服务器响应成功:" + cah); } InputStream inputStream = conn.getInputStream(); int len = 0; byte[] data = new byte[1024]; while ((len = inputStream.read(data)) != -1) { outStream2.write(data, 0, len); } outStream2.close(); inputStream.close(); String responseStr = new String(outStream2.toByteArray()); Log.v("geek", "data = " + responseStr); return responseStr; } catch (Exception e) { return ""; } } /** * 把用户的相关信息发送到服务器 * json格式 * 从服务器上获取指定的内容-POST * */ public static String postDataToServer(String svrUrl, String ParamStr) { try { // 转成指定格式 byte[] requestData = ParamStr.getBytes("UTF-8"); HttpURLConnection conn = null; DataOutputStream outStream = null; String MULTIPART_FORM_DATA = "multipart/form-data"; // 构造一个post请求的http头 URL url = new URL(svrUrl); // 服务器地址 conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // 允许输入 conn.setDoOutput(true); // 允许输出 conn.setUseCaches(false); // 不使用caches conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA); conn.setRequestProperty("Content-Length", Long.toString(requestData.length)); // 请求参数内容, 获取输出到网络的连接流对象 outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(requestData, 0, requestData.length); outStream.flush(); outStream.close(); ByteArrayOutputStream outStream2 = new ByteArrayOutputStream(); int cah = conn.getResponseCode(); Log.v("zd", "getResponseCode = " + cah); if (cah != 200) { return "0"; }else if(cah == 200){ Log.v("zd", "upload succeed"); } InputStream inputStream = conn.getInputStream(); int len = 0; byte[] data = new byte[1024]; while ((len = inputStream.read(data)) != -1) { outStream2.write(data, 0, len); } outStream2.close(); inputStream.close(); String responseStr = new String(outStream2.toByteArray()); Log.v("geek", "responseStr = " + responseStr); return responseStr; } catch (Exception e) { return "0"; } } }
new HttpUtils(); result = HttpUtils.getDataFromServer(url, jsonArray.toString());
本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1665651
标签:http协议
原文地址:http://670176656.blog.51cto.com/4500575/1665651