我正在尝试用kotlin编写的facebook登录。以下代码工作正常:
包social.social
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.facebook.*
import com.facebook.login.LoginResult
import com.facebook.login.LoginManager
import com.facebook.login.widget.LoginButton
import org.json.JSONObject
import java.util.*
class MainActivity : AppCompatActivity() {
private var callbackManager: CallbackManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
callbackManager = CallbackManager.Factory.create()
var loginBtn: LoginButton = findViewById(R.id.login_button) as LoginButton
loginBtn.setReadPermissions(Arrays.asList("public_profile","email","user_photos"))
loginBtn.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onCancel() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onError(error: FacebookException) {
print("error" +error.message)
}
override fun onSuccess(result: LoginResult) {
GraphRequest.newMeRequest(result.accessToken, GraphRequest.GraphJSONObjectCallback {
obj: JSONObject, response: GraphResponse ->
println(obj.toString())
println(obj.getString("email"))})
println("successful" + result.accessToken)
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
callbackManager!!.onActivityResult(requestCode, resultCode, data)
}
}
我尝试向GraphRequest添加参数,所以我需要将它存储到一个变量中:
val request: GraphRequest = GraphRequest.newMeRequest(result.accessToken, GraphRequest.GraphJSONObjectCallback {
obj: JSONObject, response: GraphResponse ->
println(obj.toString())
println(obj.getString("email"))})
request.executeAsync()
但现在它无法编译
错误:任务':app:compileDebugJavaWithJavac'执行失败。
无法读取类文件:'C:\ projects \ social \ app \ build \ tmp \ kotlin-classes \ debug \ social \ social \ MainActivity $ onCreate $ 1 $ onSuccess $ 1.class'
答案 0 :(得分:0)
我是Kotlin初学者,但似乎Kotlin Code for for graph request已经创建了一个编译错误。所以,最好用Java编写它并将其转换为Kotlin并重建+清理你的代码或者更新Kotlin插件。