终端对其进行编码:URLEncoder.encode(tOrderInfo, "utf-8")
// 终端工具类
package com.yuantiao.fxpark.utils;
import android.os.Handler;
import android.util.Log;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.yuantiao.fxpark.model.Order;
/**
* Created by mjc on 2017-10-19.
*/
public class HttpUtill {
private static final String TAG = "HttpUtill";
private static String tag = "NetUtils";
public static String loginOfPost(String data,String serverUrll) {
HttpURLConnection conn = null;
try {
URL url = new URL(serverUrll);
conn = (HttpURLConnection) url.openConnection();
//设置请求体的类型是文本类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求体的长度
//conn.setRequestProperty("Content-Length", String.valueOf(data.length()+2));
System.out.println("url="+url);
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
// 只有设置为TRUE,才能运行程序进行输入。
conn.setDoOutput(true);
System.out.println("data="+data);
// 获得一个输出流,向服务器写数据,默认情况下,不允许程序向服务器输出数据
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(tag, "网络连接失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
public static String loginOfGet(String data,String serverUrl) {
HttpURLConnection conn = null;
try {
// String data = "username=" + URLEncoder.encode(userName) +
// "&password=" + URLEncoder.encode(password);
// String data = "username=" + userName + "&password=" + password;
// URL url = new URL("http://192.168.0.13:8080/ServerXXX/servlet/LoginServlet?" + data);
URL url = new URL(serverUrl+"?v="+ data);
System.out.println("url="+url);
conn = (HttpURLConnection) url.openConnection();
//设置请求体的类型是文本类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求体的长度
conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
// get或者post必须得全大写
conn.setRequestMethod("GET");
// 连接的超时时间
conn.setConnectTimeout(10000);
// 读数据的超时时间
conn.setReadTimeout(5000);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
//连接成功
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(tag, "访问失败: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect(); // 关闭连接
}
}
return null;
}
public static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String result = baos.toString();
is.close();
baos.close();
return result;
}
}
import android.os.Handler;
import android.util.Log;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.yuantiao.fxpark.model.Order;
/**
* Created by mjc on 2017-10-19.
*/
public class HttpUtill {
private static final String TAG = "HttpUtill";
private static String tag = "NetUtils";
public static String loginOfPost(String data,String serverUrll) {
HttpURLConnection conn = null;
try {
URL url = new URL(serverUrll);
conn = (HttpURLConnection) url.openConnection();
//设置请求体的类型是文本类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求体的长度
//conn.setRequestProperty("Content-Length", String.valueOf(data.length()+2));
System.out.println("url="+url);
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
// 只有设置为TRUE,才能运行程序进行输入。
conn.setDoOutput(true);
System.out.println("data="+data);
// 获得一个输出流,向服务器写数据,默认情况下,不允许程序向服务器输出数据
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(tag, "网络连接失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
public static String loginOfGet(String data,String serverUrl) {
HttpURLConnection conn = null;
try {
// String data = "username=" + URLEncoder.encode(userName) +
// "&password=" + URLEncoder.encode(password);
// String data = "username=" + userName + "&password=" + password;
// URL url = new URL("http://192.168.0.13:8080/ServerXXX/servlet/LoginServlet?" + data);
URL url = new URL(serverUrl+"?v="+ data);
System.out.println("url="+url);
conn = (HttpURLConnection) url.openConnection();
//设置请求体的类型是文本类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求体的长度
conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
// get或者post必须得全大写
conn.setRequestMethod("GET");
// 连接的超时时间
conn.setConnectTimeout(10000);
// 读数据的超时时间
conn.setReadTimeout(5000);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
//连接成功
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(tag, "访问失败: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect(); // 关闭连接
}
}
return null;
}
public static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String result = baos.toString();
is.close();
baos.close();
return result;
}
}
// 终端请求
String s = null;
try {
s = HttpUtill.loginOfPost(URLEncoder.encode(qOrderInfo, "utf-8")," http://172.27.35.5:8080/ipcmsys/terminal/carsout.html");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("sss="+s);
// if(s!=null){
// cancelProgressDialog();
// }
return s;
try {
s = HttpUtill.loginOfPost(URLEncoder.encode(qOrderInfo, "utf-8")," http://172.27.35.5:8080/ipcmsys/terminal/carsout.html");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("sss="+s);
// if(s!=null){
// cancelProgressDialog();
// }
return s;
// 后台公共方法
public String getJsonParam(HttpServletRequest request) throws IOException{
log.info("开始读取数据流");
ServletInputStream in = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
log.info("reader对象是否为空:"+reader.toString());
try {
sb.append(reader.readLine()); // 按行读取,只读取一行 支持的长度是什么 post不影响
log.info("传入的参数是:"+sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
log.info("开始读取数据流");
ServletInputStream in = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
log.info("reader对象是否为空:"+reader.toString());
try {
sb.append(reader.readLine()); // 按行读取,只读取一行 支持的长度是什么 post不影响
log.info("传入的参数是:"+sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
服务器解码:String paramJson= this.getJsonParam(request);
paramJson = URLDecoder.decode(paramJson, "utf-8");
然后以流的形式传出:
PrintWriter out = response.getWriter();
out.println(termInterfaceService.carsOut(paramJson));
out.flush();
out.close();