public static void getRequestParam() throws Exception {
InputStream in = null;
String token = "";
String openId = "";
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+token+"&openid="+openId+"&lang=zh_CN";
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoInput(true);
in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = "";
StringBuffer buffer = new StringBuffer();
while((line = reader.readLine())!=null){
buffer.append(line);
}
String result = buffer.toString();
JSONObject json = JSONObject.fromObject(result);
String nickname = json.get("nickname").toString();
System.out.println(nickname);
InputStream in = null;
String token = "";
String openId = "";
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+token+"&openid="+openId+"&lang=zh_CN";
URL u = new URL(url);
//import java.net.HttpURLConnection;注意引用
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoInput(true);
in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = "";
StringBuffer buffer = new StringBuffer();
while((line = reader.readLine())!=null){
buffer.append(line);
}
String result = buffer.toString();
JSONObject json = JSONObject.fromObject(result);
String nickname = json.get("nickname").toString();
System.out.println(nickname);
}
方法中的部分代码可以提取出公用方法