新浪微博,用户信息请求response.getStatusLine().getStatusCode()返回403
刚刚接触Android应用程序,遇到了一个用户信息请求的问题:
下面是一段请求代码:
public User getUserInfo(User user) {
StringBuilder buffer = null;
// String url = "https://api.weibo.com/2/users/show.json";//新版
String url = "http://api.t.sina.com.cn/users/show.json";//旧版
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("source", oAuth.APP_KEY));
params.add(new BasicNameValuePair("user_id", user.getUser_id()));
HttpResponse response = oAuth.signRequest(user.getToken(),user.getToken_secret(), url ,params);
Log.i("tools", "-------getUserInfo--请求返回的验证码------"+(int)response.getStatusLine().getStatusCode());
....
}
上面的"-------getUserInfo--请求返回的验证码------"日志返回结果是: 403
导致其它操作无法执行!
这段代码在昨天运行请求返回的是200,今天一运行就出错了,返回变成了403
求前辈们分析一下
函数参数user的是用id=null, user_id=2598494867, user_name=null, token=89c2d7fead33a666bbfe694327651b9b, token_secret=e6e1916f037d9de838d7df959d101616, description=null, user_head=null加工出来的
oAuth调用的signRequest方法如下:
public HttpResponse signRequest(String token, String tokenSecret,
String url, List params) {
HttpPost post = new HttpPost(url);
ByteArrayOutputStream bos = null;
String file = null;
try {
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
for (int i = 0; i < params.size(); i++) {
BasicNameValuePair nameValuePair = (BasicNameValuePair) params
.get(i);
if (nameValuePair.getName().equals("pic")) {
file = nameValuePair.getValue();
}
}
byte[] data = null;
bos = new ByteArrayOutputStream(1024 * 50);
if (!TextUtils.isEmpty(file)) {
paramToUpload(bos, params);
post.setHeader("Content-Type", MULTIPART_FORM_DATA+ "; boundary=" + BOUNDARY);
Bitmap bf = BitmapFactory.decodeFile(file);
imageContentToUpload(bos, bf);
data = bos.toByteArray();
ByteArrayEntity formEntity = new ByteArrayEntity(data);
post.setEntity(formEntity);
}
} catch (Unsupported
EncodingException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (
IOException e) {
e.printStackTrace();
}
}
}
post.getParams().setBooleanParameter(
CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
return signRequest(token, tokenSecret, post);