java吧 关注:1,185,846贴子:12,590,310
  • 22回复贴,共1

android提交数据后,得到的返回值为一个对象该怎么写?

只看楼主收藏回复

public static String sendGetRequest(String path, Map<String, String> 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<String, String> 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方法该怎么写呢?求大神教教俺啊!


IP属地:广东1楼2015-01-06 09:52回复
    大神们。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


    IP属地:广东2楼2015-01-06 09:55
    回复
      、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、


      IP属地:广东3楼2015-01-06 10:04
      回复
        、、、、、、、、、、、、、、、、、、、、、、


        IP属地:广东4楼2015-01-06 10:08
        回复
          你把它解析成一个对象不就好了?


          IP属地:广东5楼2015-01-06 10:09
          收起回复
            我怎么蒙B了呢?你是用什么来传输手机端与服务端的数据的啊?JSON,xml等等。。。


            IP属地:吉林6楼2015-01-06 10:11
            收起回复
              、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、


              IP属地:广东7楼2015-01-06 10:24
              回复
                大神么来啊


                IP属地:广东8楼2015-01-06 10:38
                回复
                  求助、求助、求助


                  IP属地:广东9楼2015-01-06 15:42
                  回复
                    求助、求助、求助


                    IP属地:广东10楼2015-01-06 16:20
                    回复
                      求大神。、、、、、、、、、


                      IP属地:广东11楼2015-01-06 16:26
                      回复
                        既然都是字符串了,json对象和字符串就互相转呗


                        12楼2015-01-06 16:42
                        收起回复
                          大神在哪儿???????


                          IP属地:广东13楼2015-01-07 09:15
                          回复