开发过程 遇到天坑:纯android工程没问题,集成到unity3d中 就老提示 没登陆 。最后跟到底 发现是Util.java 中 openUrl 函数出的bug。unity3d 中调android的HttpURLConnection 必须把整个请求放在Thread中,不过本文打好的renren.jar已解决此问题(要用人人sdk源码封sdk的童鞋们注意更改以下函数)
static String mString ="a";
/**
* 发送http请求
*
* @param url
* @param method GET 或 POST
* @param params
* @return
*/
public static String openUrl(final String url, final String method, final Bundle params) {
// if (method.equals("GET")) {
// url = url + "?" + encodeUrl(params);
// }
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
Log.d(LOG_TAG, method + " URL: " + url);
InputStream is = null;
conn.setRequestProperty("User-Agent",USER_AGENT_SDK);
if (!method.equals("GET")) {
conn.setDoOutput(true);
conn.setRequestMethod("POST");
Log.d("openUrl", "33333333333::::"+ encodeUrl(params).getBytes("UTF-8") );
conn.getOutputStream().write(encodeUrl(params).getBytes("UTF-8"));
Log.d("openUrl", "44444444444444::::");
}
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
is = conn.getInputStream();
} else {
is = conn.getErrorStream();
}
mString = read(is);
Log.d("openUrl", "101010101010101"+mString );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Log.e("mString ", mString);
return mString;
}
另外 作者把官方繁琐的demo 做了修改 简单实现调用人人 实现图文分享功能
主类代码:
public class RenrenSDKDemo extends UnityPlayerActivity { private static final String API_KEY = "6b1016db20c540e78bd1b20be4c707a3"; private static final String SECRET_KEY = "4723a695c09e4ddebbe8d87393d95fb4"; private static final String APP_ID = "105381"; private Renren renren; private Handler handler; RenrenAuthListener listener ; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
renren = new Renren(API_KEY, SECRET_KEY, APP_ID, this);
//Initialize the demo invoker
ApiDemoInvoker.init(renren);
renren.init(this);
handler = new Handler();
initButtons();
} @Override
protected void onResume() {
super.onResume(); } private final static int DATA_COMPLETE = 0xffff;
private final static int DATA_ERROR = 0xfffe;
private final static int DATA_FAULT = 0xfffd;
private final static String ERROR_MSG = "error_message";
/**
* start the api list activity
*/
private void startApiList() {
PhotoUploadRequestParam photoParam = new PhotoUploadRequestParam();
PhotoHelper photoHelper = new PhotoHelper(renren);
photoParam.setCaption("疯狂攀岩家 http://www.baidu.com");
photoParam.setFile(new File("/sdcard/myImage/data.png"));
photoHelper.asyncUploadPhoto(photoParam,
new AbstractRequestListener<PhotoUploadResponseBean>() {
@Override
public void onRenrenError(RenrenError renrenError) {
if (renrenError != null) {
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString(ERROR_MSG,
renrenError.getMessage());
message.what = DATA_ERROR;
message.setData(bundle);
handler1.sendMessage(message); Util.logger("exception in uploading photo: "
+ renrenError.getMessage());
}
} @Override
public void onFault(Throwable fault) {
if (fault != null) {
handler1.sendEmptyMessage(DATA_FAULT); Util.logger("fault in uploading photo: "
+ fault.getMessage());
}
} @Override
public void onComplete(
PhotoUploadResponseBean photoResponse) {
if (photoResponse != null) {
handler1.sendEmptyMessage(DATA_COMPLETE); Util.logger("sucess uploading photo! \n"
+ photoResponse);
}
}
});
} /**
* initialize the buttons and events
*/
private void initButtons() {
listener = new RenrenAuthListener() { @Override
public void onComplete(Bundle values) {
Log.d("test",values.toString());
Log.e("AAAAAAAAAAAAA", "WWWWWWWWWW");
authorize();
} @Override
public void onRenrenAuthError(
RenrenAuthError renrenAuthError) {
handler.post(new Runnable() { @Override
public void run() {
Toast.makeText(RenrenSDKDemo.this,
RenrenSDKDemo.this.getString(R.string.auth_failed),
Toast.LENGTH_SHORT).show();
}
});
} @Override
public void onCancelLogin() {
} @Override
public void onCancelAuth(Bundle values) {
} };
} public void ShareRenRen()
{
Message msgMessage =new Message();
msgMessage.what = 0;
handler1.sendMessage(msgMessage);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (renren != null) {
renren.authorizeCallback(requestCode, resultCode, data);
Log.e("onActivityResult", "SSSSSSSSSSSSSSS");
}
} Handler handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg); switch (msg.what) {
case DATA_COMPLETE:
showTip("上传成功"); break;
case DATA_ERROR: Bundle bundle = msg.getData();
String errorMessage = "上传失败";
if (bundle != null) {
String error = bundle.getString(ERROR_MSG);
if (error != null || !"".equals(error)) {
errorMessage = "," + error;
}
}
showTip(errorMessage); break;
case DATA_FAULT: showTip("上传失败"); break;
case 0:
Log.e("ShareRenRen", "SSSSSSSSSSSSSSS");
renren.authorize(RenrenSDKDemo.this, listener); break;
default:
break;
}
}
}; /**
* 显示Toast提示
*
* @param message
*/
protected void showTip(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
private void authorize() {
AuthorizationHelper.check(renren, RenrenSDKDemo.this,
new String[] { PhotoHelper.UPLOAD_PHPTO_PERMISSION },
new RenrenAuthListener() { @Override
public void onRenrenAuthError(
RenrenAuthError renrenAuthError) {
finish();
} @Override
public void onComplete(Bundle values) {
startApiList();
} @Override
public void onCancelLogin() {
finish();
} @Override
public void onCancelAuth(Bundle values) {
finish();
}
});
} }
public void ShareRenRen() //unity3d调用的入口
{
Message msgMessage =new Message();
msgMessage.what = 0;
handler1.sendMessage(msgMessage);
}
因为直接调用会出错 作者通过在handler里面实现具体函数调用
android工程下载地址 http://vdisk.weibo.com/s/IzsAo