下面的System.out.println语句总是向logcat输出一个哈希引用,如“HASH(0x ....)”,无论我是否指定要打印的变量为“line”或“line.toString”( )”。如何让它打印实际的字符串值?
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
try {
con.setRequestMethod("POST");
} catch (ProtocolException e) {
e.printStackTrace();
}
OutputStreamWriter wr;
try {
wr = new OutputStreamWriter(con.getOutputStream());
wr.write(json.toString());
wr.flush();
} catch (IOException e) {
e.printStackTrace();
}
int HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println("Line: " + line.toString());
}
br.close();
}
答案 0 :(得分:0)
您的代码没有任何问题。你可以用更简单的东西替换循环上面的所有代码,例如:
FileInputStream fstream = new FileInputStream("somefile");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream,"utf-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println("Line: " + line);
}
br.close();
您将看到该文件的内容。
请注意,我不需要执行.toString()
,因为该变量已经是一个字符串。
我怀疑这里是服务器发送一个压缩的主体,每一行都是一个哈希,因为这是压缩的定义,全部是十六进制。
内容是json,但它被压缩以改善大小和速度。