我正在尝试将身份验证代码交换为访问令牌。
我正在使用AsyncTaskRunner
,但是出现错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at java.net.URLEncoder.encode(URLEncoder.java:205).
这是我的请求参数:
CLIENT_ID = "1fb7c350-bb6a-5741-86b9-43afc2f1642f";
REDIRECT_URI = "https://xxxx.xxxxx.xxxx/rest/callback.html";
CLIENT_SECRET = "xxxxxxx";
GRANT_TYPE = "authorization_code";
ACCESS_TOKEN_URL = "https://xxxx.xxxxxx.com/oauth/token";
我的AsyncTask类
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
try {
URL url = null;
InputStream stream = null;
HttpURLConnection urlConnection = null;
try {
url = new URL(ACCESS_TOKEN_URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
data = URLEncoder.encode("client_id", "UTF-8")
+ "=" + URLEncoder.encode(CLIENT_ID, "UTF-8"); //*This line above is where the caused by exception is pointing to.*
data += "&" + URLEncoder.encode("client_secret", "UTF-8") + "="
+ URLEncoder.encode(CLIENT_SECRET, "UTF-8");
data += "&" + URLEncoder.encode("grant_type", "UTF-8") + "="
+ URLEncoder.encode(GRANT_TYPE, "UTF-8");
data += "&" + URLEncoder.encode("code", "UTF-8") + "="
+ URLEncoder.encode(authCode, "UTF-8");
data += "&" + URLEncoder.encode("redirect_uri", "UTF-8") + "="
+ URLEncoder.encode(REDIRECT_URI, "UTF-8");
urlConnection.connect();
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(data);
wr.flush();
stream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 8);
String result = reader.readLine();
accessToken = result;
return accessToken;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
Log.i("Result", "SLEEP ERROR");
}
return null;
} finally {
Log.i("Result", "SLEEP ERROR");
}
}
}
在我的发送按钮上:
authCode = sharedPreferences.getString("code", null);
params=new String[5];
CLIENT_ID = params[0];
CLIENT_SECRET = params[1];
GRANT_TYPE = params[2];
REDIRECT_URI = params[3];
authCode = params[4];
new AsyncTaskRunner().execute(params);
这是我的完整日志:
07-04 11:43:57.749 7157-8120/almac.com.cameratutorialtest E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: almac.com.cameratutorialtest, PID: 7157
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.net.URLEncoder.encode(URLEncoder.java:205)
at almac.com.cameratutorialtest.MainActivity$AsyncTaskRunner.doInBackground(MainActivity.java:275)
at almac.com.cameratutorialtest.MainActivity$AsyncTaskRunner.doInBackground(MainActivity.java:256)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
在上面进行设置后,它如何变为空?为何它还没有检索访问令牌呢?
答案 0 :(得分:2)
替换
authCode = params[4];
使用
params[4] = authCode
就在这里
authCode = sharedPreferences.getString("code", null);
params=new String[5];
CLIENT_ID = params[0];
CLIENT_SECRET = params[1];
GRANT_TYPE = params[2];
REDIRECT_URI = params[3];
authCode = params[4];
new AsyncTaskRunner().execute(params);`
您正在将每个变量设置为null,这不是您想要的,这就是为什么会出现nullpointer错误的原因。