您的位置:首页 > 移动开发 > 微信开发

微信根据openId查询微信用户信息,需要认证服务号。

2014-01-03 13:53 621 查看
import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

public static JSONObject getWechatUserInfo(String openId,String taken){

String url = WechatAppConfig.wechat_tx_url+"/cgi-bin/user/info?access_token="+taken+"&openid="+openId;
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(url);
       try {
       
client.executeMethod(get);
       
String returnJson=get.getResponseBodyAsString();
       
returnJson = new String(returnJson.getBytes("ISO8859_1"),"UTF-8");
       
JSONObject result = JSONObject.fromObject(returnJson);
       

       
if(result.containsKey("nickname"))
       
return new JsonResult(true).putObject(result).getResult();
       
else
       
return new JsonResult(false,result.optString("errcode")+":"+result.optString("errmsg")).getResult();
       }
       catch (Exception e) {
       

       
return new JsonResult(false,e.getMessage()).getResult();
       }

return token;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息