android客户端通过GET方式发送数据到服务端,服务端获得数据后,从服务端获取数据库里的信息,并以JSON数据格式返回。
1、GET方式传参的格式:
http://127.0.0.1/AndroidService/android/upload?title=aaa&timelength=90的形式
参数是?后的title=aaa&timelength=90。多个参数用&连接。
2、连接服务器发送请求参数并获得服务器返回的数据,客户端获得数据后,主要是对JSON数据的一些解析。
/**
* 获得服务器的数据
* @param url
* @return
*/
public static String connect(URL url){
InputStream inputStream=null;
HttpURLConnection connection=null;
StringBuffer sb=null;
try {
connection=(HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
if(connection.getResponseCode()==200){
inputStream=connection.getInputStream();
//对应的字符编码转换
Reader reader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String str = null;