最近新学,想问一下各位大神,从android怎么发送一串json到服务器的一个servlet上,并且servlet是如何接收到这个jsonn后再返回一条json给android,最后由android接收到这个返回的json.
(网上查的东西太乱了,想知道怎么有android吧数据放到request病发送到服务器在由服务器接收并通过response返回的) 谢谢各位了
------解决方案--------------------
采用gson这个jar包吧,非常方便。
------解决方案--------------------
这里android和web没有区别,把需要传递的数据封装成对象,然后转换成字符串通过post传递。
下面附上封装好的几个常用方法:
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.Gson;
/**
* json字符串 和对象互换 工具类
* 依赖Gson包
* @see com.google.gson.Gson
* @author devil
*
*/
public class JsonUtil {
/**
* 将json字符串转换成对象
*
* @param json
* @param type
* @return
*/
public static <T> T parse(String json, Class<T> type) {
Gson gson = new Gson();
T t = null ;
try {
t = gson.fromJson(json, type) ;
} catch (Exception e) {
e.printStackTrace() ;
return null ;
}
return t;
}
/**
* 将json转成数组
*
* @param json
* @param type
* @return
*/
public static <T> T[] parseArr(String json, Class<T[]> type) {
return parse(json, type);
}
/**
* 将json转成集合
*
* @param json
* @param type
* @return
*/
public static <T> ArrayList<T> parseList(String json, Class<T[]> type) {
return new ArrayList<T>(Arrays.asList(parse(json, type)));
}
/**
* 将对象转成json字符串
*
* @param o
* @return
*/
public static String format(Object o) {
Gson gson = new Gson();
return gson.toJson(o);
}
/**
* 将对象转成json字符串 并使用url编码
* @param o
* @return
*/
public static String formatURLString(Object o)
{
try
{
return URLEncoder.encode(format(o), "utf-8");
}catch (Exception e) {
return null;
}
}
}
------解决方案--------------------
http请求网上代码一大堆。
public static String postHttpResponseText(String url, String post) {
BufferedReader reader = null;
HttpURLConnection conn = null;
try {
URL httpUrl = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) httpUrl
.openConnection(); // //设置连接属性
httpConn.setDoOutput(true);// 使用URL 连接进行输出
httpConn.setDoInput(true);// 使用 URL 连接进行输入
httpConn.setUseCaches(false);// 忽略缓存
httpConn.setRequestMethod("POST");// 设置URL请求方法
String requestString = post; // 设置请求属性
// 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
byte[] requestStringBytes = requestString.getBytes("UTF-8");
httpConn.setRequestProperty("Content-length", ""
+ requestStringBytes.length);
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
// 建立输出流,并写入数据
OutputStream outputStream = httpConn.getOutputStream();
outputStream.write(requestStringBytes);
outputStream.close(); // 获得响应状态
int responseCode = httpConn.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功 //
// 当正确响应时处理数据
StringBuffer buffer = new StringBuffer();
String readLine = null;
// 处理响应流,必须与服务器响应流输出的编码一致
reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream(), "UTF-8"));
while ((readLine = reader.readLine()) != null) {
//buffer.append(readLine).append("\n");
buffer.append(readLine);
}
reader.close();
return buffer.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
if (conn != null) {
conn.disconnect();
}
}
return null;
}这是自己写的,url是请求地址,post是参数
------解决方案--------------------
需要传递参数的,javaBean知道吧,给javaBean的各个字段赋值,然后调用JsonUtil.format(Object obj)方法转换成String字符串,这个字符串就是post参数,如果没有参数直接post那直接写null或""就行。