如何在Android中使用Authorization标头?

时间:2015-05-14 08:08:04

标签: java android

我是Android新手。我想从后端获取所有图像    服务器和使用它的API,如GET   (https://api.xxxx.com/xxxx/images/search?category=1&&&&&&image_type=photo&&license=commercial&&&&per_page=100&&&&&&&&sort=popular&view=minimal   )。

授权标题。 client_id是。{    2484a01edd7888369c2eb client_secret是    8n12d6f0bb5de345c2f4b1e406504d3b95313d5任何人都可以帮助我    使用此授权标头信息。谢谢    升值。

这是我的活动代码

public class MainActivity extends Activity
{
    String strUrl = "https://api.shutterstock.com/v2/images/search?category=1&&&&&&image_type=photo&&license=commercial&&&&per_page=100&&&&&&&&sort=popular&view=minimal";
    String client_id = "2484a01edd177369c2eb";
    String client_secret = "8b12d6f0bb5de2875c2f4b1e406504d3b95313d5";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GetImagesFromServer();
    }

    public void GetImagesFromServer()
    {

        HttpUriRequest request = new HttpGet(strUrl); // Or HttpPost(), depends on your needs  
        String credentials = client_id + ":" + client_secret;
        String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);  
        request.addHeader("Authorization", "Basic " + base64EncodedCredentials);

        try 
        {
            HttpClient httpclient = createHttpClient();
            HttpResponse response = httpclient.execute(request);
            Log.e("Response"," = " + response.toString());
        } 
        catch (ClientProtocolException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();}

    }

    public static HttpClient createHttpClient()
    {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setUseExpectContinue(params, true);

        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

        return new DefaultHttpClient(conMgr, params);
    }

}

这是我的Log cat错误信息

05-14 12:30:46.928: W/System.err(416): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
05-14 12:30:46.928: W/System.err(416):  at org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:258)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:164)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-14 12:30:46.938: W/System.err(416):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-14 12:30:46.938: W/System.err(416):  at com.example.imagedemo.MainActivity.GetImagesFromServer(MainActivity.java:45)
05-14 12:30:46.938: W/System.err(416):  at com.example.imagedemo.MainActivity.onCreate(MainActivity.java:29)
05-14 12:30:46.938: W/System.err(416):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-14 12:30:46.938: W/System.err(416):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-14 12:30:46.948: W/System.err(416):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-14 12:30:46.948: W/System.err(416):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-14 12:30:46.948: W/System.err(416):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-14 12:30:46.948: W/System.err(416):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 12:30:46.948: W/System.err(416):  at android.os.Looper.loop(Looper.java:123)
05-14 12:30:46.948: W/System.err(416):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-14 12:30:46.948: W/System.err(416):  at java.lang.reflect.Method.invokeNative(Native Method)
05-14 12:30:46.948: W/System.err(416):  at java.lang.reflect.Method.invoke(Method.java:507)
05-14 12:30:46.948: W/System.err(416):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-14 12:30:46.948: W/System.err(416):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-14 12:30:46.948: W/System.err(416):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案