0

这是我的网址帖子:

    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://callback

<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());

这会产生错误。

4

1 回答 1

1

您可能需要设置请求类型,并指定您需要输入流。在尝试使用输入流之前,您还应该检查请求的响应代码。

您在下面发布的异常表明您的服务器使用自定义 ssl 证书导致出现问题。您可能需要创建 HttpsURLConnection 可以使用的本地 ssl 信任存储。这篇博客文章介绍了一些关于如何做到这一点的技术,一旦你完成了,你就可以从你的连接中使用它

就像是

HttpsURLConnection yc = (HttpsURLConnection)pocketUrl.openConnection();
yc.setDoInput(true);
yc.setRequestMethod("GET");
yc.setSSLSocketFactory(sslContext.getSocketFactory());
if (urlConnection.getResponseCode() != 200) {
    // handle error here
    final String responseMessage = yc.getResponseMessage();
    Log.e(TAG, "Failed Response: " + responseMessage);
    return;
}

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
于 2012-12-06T21:36:03.513 回答