gpt4 book ai didi

java - 使用 BackgroundTask 和 Facebook 的上下文

转载 作者:行者123 更新时间:2023-11-30 01:28:46 25 4
gpt4 key购买 nike

我的 android 应用程序有问题。如果我使用我的“应用程序帐户”访问就可以了。

否则,如果我使用我的 Facebook 帐户凭证访问,我的上下文为空。

如何解决?

下面是我的代码。提前谢谢你。

登录.java :

public class Login extends AppCompatActivity {

Context ctx;
private LoginButton loginButtonFb;
private TextView info;
private CallbackManager callbackManager;
EditText ET_NAME, ET_PASS;
String login_name, login_pass;
static final String USERNAME = "USER_NAME";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
loginButtonFb = (LoginButton)findViewById(R.id.login_button_fb);
loginButtonFb.setReadPermissions("user_friends");

ET_NAME = (EditText) findViewById(R.id.user_name);
ET_PASS = (EditText) findViewById(R.id.user_pass);
info = (TextView)findViewById(R.id.info);

loginButtonFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {

new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId(),
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject data = response.getJSONObject();
String idFb = data.getString("id");
String nameFb = data.getString("name");
String method = "loginFb";
BackgroundTask backgroundTask = new BackgroundTask(ctx);
backgroundTask.execute(method, nameFb, idFb);
} catch (JSONException e) {
e.printStackTrace();
}

}
}
).executeAsync();

}

@Override
public void onCancel() {
info.setText("Login attempt canceled.");
}

@Override
public void onError(FacebookException e) {
info.setText("Login attempt failed.");
}
});

}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}


public void userReg(View view){

Intent intent = new Intent(this, Registration.class);
startActivity(intent);
}



private class BackgroundTask extends AsyncTask<String, Void, String> {

Context ctx;

BackgroundTask(Context ctx){
this.ctx = ctx;
}

@Override
protected String doInBackground(String... params) {
String method = params[0];
if (method.equals("login")){
String login_url = "http://192.168.1.118:80/webappdb/login.php";
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" +
URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();

InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader((new InputStreamReader(inputStream, "iso-8859-1")));
String response = "";
String line = "";
while((line = bufferedReader.readLine()) != null){
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

else if(method.equals("loginFb")){
String login_name = params[1];
String id = params[2];
String loginFb_url = "http://192.168.1.118:80/webappdb/loginFb.php";
try {
URL url = new URL(loginFb_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

String data = URLEncoder.encode("login_nameFb", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" +
URLEncoder.encode("id", "UTF-8") + "=" + URLEncoder.encode(id, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();

InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader((new InputStreamReader(inputStream, "iso-8859-1")));
String response = "";
String line = "";
while((line = bufferedReader.readLine()) != null){
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
return null;
}



@Override
protected void onPostExecute(String result) {

if(result.equals("Login failed, try again.")){

Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
if (result.equals("Login success!")){
Intent intent = new Intent(ctx,UserProfile.class);
intent.putExtra(USERNAME, login_name);
startActivity(intent);

Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}

}
}




public void userLogin(View view){

login_name = ET_NAME.getText().toString();
login_pass = ET_PASS.getText().toString();
String method = "login";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, login_name, login_pass);

}





}

堆栈跟踪:

gamingproject.sellmybooks E/AndroidRuntime: FATAL EXCEPTION: main
Process: gamingproject.sellmybooks, PID: 30637
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4144)
at gamingproject.sellmybooks.Login$BackgroundTask.onPostExecute(Login.java:228)
at gamingproject.sellmybooks.Login$BackgroundTask.onPostExecute(Login.java:123)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5536)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

最佳答案

在您的登录 Activity 中,更改'上下文 ctx' 到 '上下文 mContext'当 onCreate() 开始时,做

 mContext = this; 

像那样:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
...
}

在您的方法 userLogin 上,发送“mContext”

public void userLogin(View view){

login_name = ET_NAME.getText().toString();
login_pass = ET_PASS.getText().toString();
String method = "login";
BackgroundTask backgroundTask = new BackgroundTask(mContext);
backgroundTask.execute(method, login_name, login_pass);

}

关于java - 使用 BackgroundTask 和 Facebook 的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com