package Htttp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class HttpUtils_touxiang {
public HttpUtils_touxiang() {
}
public static Touxiang getJsonContent(String url_path){
try {
URL url = new URL(url_path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
int code = connection.getResponseCode();
if(code ==200){
return changeInputStream(connection.getInputStream());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static Touxiang changeInputStream(InputStream inputStream) {
// TODO Auto-generated method stub
String jsonString = "";
String nickname = null;
String headimgurl =null;
Touxiang touxiang = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = 0;
byte[] data = new byte[1024];
try {
while((len = inputStream.read(data))!=-1){
outputStream.write(data,0,len);
}
jsonString = new String(outputStream.toByteArray());
//到这里已经是乱码了
System.out.println("先前返回的是:"+jsonString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonString);
System.out.println("返回的是:"+jsonObject);
nickname = jsonObject.getString("nickname").toString();
headimgurl = jsonObject.getString("headimgurl");
touxiang = new Touxiang();
touxiang.setHeadimgurl(headimgurl);
touxiang.setNickname(nickname);
System.out.println("你的昵称是:"+nickname);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return touxiang;
}
}
jsonString = new String(outputStream.toByteArray());
//到这里已经是乱码了
System.out.println("先前返回的是:"+jsonString);
改成
jsonString = new String(outputStream.toByteArray(),"UTF-8");//微信接口是UTF-8格式的。
//到这里已经是乱码了
System.out.println("先前返回的是:"+jsonString);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。