该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public static String sendGetRequest(String path, Map params, String enc) throws IOException {
/*
* http://127.0.0.1/AndroidService/android/upload?title=aaa&timelength=90的形式
*/
StringBuilder sb = new StringBuilder(path);
sb.append('?');
for(Map.Entry entry : params.entrySet()) {
sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
try {
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET"); //设置方法为GET
conn.setReadTimeout(5 * 1000); //设置过期时间为5秒
if(conn.getResponseCode() == 200) { //如果成功返回
InputStream inStream = conn.getInputStream();//通过输入流获取html数据
String s2 = changeInputStream(inStream,"UTF-8");
return s2;
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(TAG, e.toString());
return "";
}
return "";
}
/**
* 将一个输入流转换成指定编码的字符串
*
* @param inputStream
* @param encode
* @return
*/
private static String changeInputStream(InputStream inputStream,
String encode) {
// 内存流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = null;
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
result = new String(byteArrayOutputStream.toByteArray(), encode);
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
我当前的changeInputStream方法返回值是返回一个字符串,我如果是返回一个对象,我这个changeInputStream方法该怎么写呢?求大神教教俺啊!