Giter VIP home page Giter VIP logo

volleyhttps's Introduction

volleyHttps

##Making a HTTPS request using Android Volley and Self Certified SSL

This is an example for using volley with SSL.

Thank for Arnab Chakraborty's article. It's very helpful.

http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

#How to use it. ###1. In AndroidManifest.xml add the VolleyHelperApplication

<application
android:name="xiaohui.volley.VolleyHelperApplication"
...
</application>

In build.gradle

dependencies {
 compile 'com.mcxiaoke.volley:library:1.0.19'
 }

#2. Example:

StringRequest - HTTP/GET

private void stringRequestGetHttpExample(){

        VolleyDataRequester.withHttp( this )
                .setUrl( HTTP_HOST + IP)
                .setMethod( VolleyDataRequester.Method.GET )
                .setStringResponseListener( new VolleyDataRequester.StringResponseListener() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText( MainActivity.this, "HTTP/POST,StringRequest successfully.", Toast.LENGTH_SHORT ).show();
                        tvResult.setText( response );
                    }
                } )
                .requestString();
    }

##StringRequst HTTP/POST

    private void stringRequestPostHttpExample(){

        HashMap<String, String> body = new HashMap <String, String>() ;
        body.put( "name", "xiaohui" );
        body.put( "gender", "male" );

        VolleyDataRequester.withHttp( this )
                .setUrl( HTTP_HOST +  POST)
                .setBody( body )
                .setMethod( VolleyDataRequester.Method.POST )
                .setStringResponseListener( new VolleyDataRequester.StringResponseListener() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText( MainActivity.this, "HTTP/POST,StringRequest successfully.", Toast.LENGTH_SHORT ).show();
                        tvResult.setText( response );
                    }
                } )
                .requestString();
    }

##JsonRequest HTTPS/GET

    private void jsonRequestGetHttpsExample(){
        VolleyDataRequester.withDefaultHttps( this )
                .setUrl(HTTPS_HOST + IP)
                .setJsonResponseListener( new VolleyDataRequester.JsonResponseListener() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            String s = response.getString( "origin" );
                            tvResult.setText( s );
                            Toast.makeText( MainActivity.this, "HTTPS/GET, JsonRequest successfully.", Toast.LENGTH_SHORT ).show();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                } )
                .setResponseErrorListener( new VolleyDataRequester.ResponseErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        tvResult.setText( error.getMessage() );
                    }
                } )
                .requestJson();
    }

##JsonRequest HTTP/POST

    private void jsonRequestPostHttpsExample(){
        JSONObject json = new JSONObject(  );
        try{
            json.put( "name", "xiaohui" );
            json.put( "gender", "male" );
        }catch (Exception e){
            e.printStackTrace();
        }

        VolleyDataRequester.withDefaultHttps( this )
                .setUrl(HTTPS_HOST + POST)
                .setBody( json )
                .setJsonResponseListener( new VolleyDataRequester.JsonResponseListener() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            String data = response.getString( "data" );
                            tvResult.setText( data);

                            Toast.makeText( MainActivity.this, "HTTPS/POST, JsonRequest successfully.", Toast.LENGTH_SHORT ).show();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                } )
                .setResponseErrorListener( new VolleyDataRequester.ResponseErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        tvResult.setText( error.getMessage() );
                    }
                } )
                .requestJson();
    }

##JsonArrayRequest HTTP/GET

    private void jsonArrayRequestGetHttpsExample(){
        VolleyDataRequester.withDefaultHttps( this )
                .setUrl(HTTPS_JSONARRAY)
                .setJsonArrayResponseListener( new VolleyDataRequester.JsonArrayResponseListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                        String output = "Array length=" + response.length();

                        for (int i = 0; i < response.length(); i++){
                            try {
                                JSONObject json = (JSONObject) response.get( i );
                                String name = json.getString( "name" );
                                output += "\n" + i + " - " + name;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }

                        tvResult.setText( output );
                    }
                } )
                .setResponseErrorListener( new VolleyDataRequester.ResponseErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        tvResult.setText( error.getMessage() );
                    }
                } )
                .requestJsonArray();
    }

#3. Using Self Certified SSL

  1. Replace the certificates client.key.p12 and client.truststore with our own certificates in folder assets
  2. Configure the CertificateConfig.java
  3. Then use the following method to send the request.
VolleyDataRequester.withSelfCertifiedHttps( this )
                .setUrl( You_url)
                .setJsonResponseListener( new YouJsonRequestListener ())
                .requestJson();

#Screeshot: Alt text

References:

http://developer.android.com/training/volley/index.html

https://github.com/yuxiaohui78/android-volley

http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800

http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

https://github.com/yuxiaohui78/Volley-demo

volleyhttps's People

Contributors

yuxiaohui78 avatar

Stargazers

CODER avatar  avatar Wenew Zhang avatar SUPRIYANTO avatar 李小义 avatar tinyjoy avatar Walker Zhao avatar  avatar ShanZha avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.