這是我的網址後:未收到來自回叫網址的任何數據。 Android在哪裏出錯?
String urlParameters = "&consumer_key="+AppSession.getPocketConsumerKey()+"&client_secret="+AppSession.getPocketRedirectUri();
String request = "https://getpocket.com/v3/oauth/request";
URL url;
try {
url = new URL(request);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("X-Accept","application/x-www-form-urlencoded");
connection.setUseCaches (false);
connection.connect();
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
Log.d("urlparmas",urlParameters);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
Log.d("response",response.toString());
} catch (Exception e) {
e.printStackTrace();
}
我的重定向URI:MyApp的://回調
<activity android:name="com.app.account.Register" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="MyApp" android:host="callback"/>
</intent-filter>
</activity>
所以,我只是沒有得到從回調URL任何答覆。想知道我在代碼中出錯的地方。
編輯:
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
Log.d("response",response.toString());
這將產生錯誤。
兩件事一些技巧:首先,是你的catch塊?最有可能的是拋出異常,會告訴你到底發生了什麼。 第二:您的清單中是否設置了Internet權限? – slund
讓我重新發布整個代碼。 – Hick
已編輯併發布整個功能。 – Hick