json解析中文的时候乱码的话是报错的。
访问其他的服务器的方法之后返回的是默认编码格式(那边没指定一个编码发送内容)
所以解析时候在文件流中解析就可以了。
Strint name = "testone";
String password = "passwordone";
URL u = new URL("http://test.com.cn?name="+name+"&password="+password+"");
HttpURLConnection uConnection = (HttpURLConnection) u.openConnection();
uConnection.connect();
InputStream is = uConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();//Stringbuffer的效率比它的低,不解释。
while (br.read() != -1) {
sb.append(br.readLine());
}
br.close();
String content = "{"+sb.toString();//通过流获取之后总是把第一个括号给省略掉,自己加一个。
JSONObject j = JSONObject.fromObject(content);
String result = (String)j.get("result");
String username = (String)j.get("name");