Sending a POST request with JSONArray using Volley

15,310

You can refer to my following sample code:

UPDATE for your pastebin link:

Because the server responses a JSONArray, I use JsonArrayRequest instead of JsonObjectRequest. And no need to override getBody anymore.

        mTextView = (TextView) findViewById(R.id.textView);
        String url = "https://api.orange.com/datavenue/v1/datasources/2595aa553d3049f0b0f03fbaeaa7ddc7/streams/9fe5edb1c76e4968bdcc9c902010bc6c/values";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final String jsonString = "[\n" +
                " {\n" +
                "  \"value\": 1\n" +
                " }\n" +
                "]";
        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, jsonArray, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    mTextView.setText(response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mTextView.setText(error.toString());
                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    headers.put("X-OAPI-Key","TQEEGSk8OgWlhteL8S8siKao2q6LIGdq");
                    headers.put("X-ISS-Key","2b2dd0d9dbb54ef79b7ee978532bc823");
                    return headers;
                }
            };
            requestQueue.add(jsonArrayRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }

My code works for both Google's official volley libray and mcxiaoke's library

If you want to use Google's library, after you git clone as Google documentation, copy android folder from \src\main\java\com (of Volley project that you cloned) to \app\src\main\java\com of your project as the following screenshot:

enter image description here

The build.gradle should contain the following

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.google.code.gson:gson:2.3.1'    
}

If your project uses mcxiaoke's library, the build.gradle will look like the following (pay attention to dependencies):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.example.samplevolley"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.mcxiaoke.volley:library:1.0.17'
    compile 'com.google.code.gson:gson:2.3'
}

I suggest that you will create 2 new sample projects, then one will use Google's library, the other will use mcxiaoke's library.

END OF UPDATE

        String url = "http://...";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final String jsonString = "[\n" +
                " {\n" +
                "  \"value\": 1\n" +
                " }\n" +
                "]";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // do something...
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // do something...
            }
        }) {
            @Override
            public byte[] getBody() {
                try {
                    return jsonString.getBytes(PROTOCOL_CHARSET);
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            jsonString, PROTOCOL_CHARSET);
                    return null;
                }
            }
        };
        requestQueue.add(jsonObjectRequest);

The following screenshot is what server-side web service received:

enter image description here

Share:
15,310
fujitsu4
Author by

fujitsu4

Updated on June 18, 2022

Comments

  • fujitsu4
    fujitsu4 almost 2 years

    I want to send a simple POST request in Android with a body equaling this :

    [
     {
      "value": 1
     }
    ]
    

    I tried to use Volley library in Android, and this is my code :

    // the jsonArray that I want to POST    
    String json = "[{\"value\": 1}]";
    JSONArray jsonBody = null;
    try {
         jsonBody = new JSONArray(json);
        } catch (JSONException e) {
                                   e.printStackTrace();
                                  }
    final JSONArray finalJsonBody = jsonBody;
    
    // starting the request
    final RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    JsonObjectRequest request = 
    new JsonObjectRequest(com.android.volley.Request.Method.POST,"https://...",null,
    
    new Response.Listener<JSONObject>() {
    
    @Override
    public void onResponse(JSONObject response) {
    Log.d("mytag", "Response is: " + response);}},
    new Response.ErrorListener() {
    
    @Override
    public void onErrorResponse(VolleyError error) {
    Log.d("Mytag", "error");}}) {
    
    @Override
    protected  Map<String,String> getParams() {
    // the problem is here...
    return (Map<String, String>) finalJsonBody;
    }
    
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError  {
    HashMap<String, String> params = new HashMap<String, String>();
    // I put all my headers here like the following one : 
    params.put("Content-Type", "application/json");                                    
    return params;}};
    
    queue.add(request);
    

    The problem is that the getParams method only accepts a Map object since I want to send a JSONArray. So, I'm obliged to use a cast, which generate an error then...

    I don't know how can I fix that Thank you

  • fujitsu4
    fujitsu4 over 8 years
    I tried your code and I get an error related to "PROTOCOL_CHARSET". It tells me : 'PROTOCOL_CHARSET' has private access in com.android.volley.toolbox.JsonRequest
  • BNK
    BNK over 8 years
    I guess that your project uses volley library by compile 'com.mcxiaoke.volley:library:1.0.17' in build.gradle file. My project uses Google's official volley library. You can replace PROTOCOL_CHARSET by "utf-8"
  • fujitsu4
    fujitsu4 over 8 years
    I am still getting an error : com.android.volley.ServerError. Perhaps it's due to volley library which is badly programmed?? Perhaps it will be a solution to take the google one?
  • BNK
    BNK over 8 years
    I think you should override parseNetworkError to find out more error details. You should also check your server-side app to see how it processed your request.
  • fujitsu4
    fujitsu4 over 8 years
    I think it's better to work with the google library instead of mcxiaoke one. Do you know how can I find their .jar directly ? (I did a git clone in my Mac but after that I always get an error after the android start project command and it's very frustrating). Testing google library will be my last attempt^^
  • BNK
    BNK over 8 years
    I don't use jar file. After you git clone as Google documentation, copy android folder inside \src\main\java\com (of Volley project that you cloned) into \app\src\main\java\com of your project. About mcxiaoke's volley, I used it already, it is good too :-)
  • fujitsu4
    fujitsu4 over 8 years
    This is the request that I want to send : pastebin.com/wt4ndxvH Can you please test if it works for you or not? please. Thank you!!!
  • BNK
    BNK over 8 years
    Ok, I will test tomorrow. You can also do your own test by a very helpful tool. That is Postman for Chrome
  • fujitsu4
    fujitsu4 over 8 years
    Ah no no, unfortunately it's not the case yet. I'm waiting for you to test if you can post the request that i showed you (i said thank you -by politeness- for all your help), but unfortunately the problem is still occuring. Sorry if my precedent message was ambiguous
  • BNK
    BNK over 8 years
    I have tested as your pastepin link, it works. Here is the screenshot link
  • BNK
    BNK over 8 years
    So, you need to add X-OAPI-Key and X-ISS-Key in the headers of the volley request.
  • fujitsu4
    fujitsu4 over 8 years
    Thank you very much!! I don't have android studio now. So, I can't test that before Monday. But I have faith this time, there is no reason to not work. Again, thank you very much!!!
  • BNK
    BNK over 8 years
    You're welcome! Don't worry, I often test OK before posting answer :-)
  • fujitsu4
    fujitsu4 over 8 years
    I tried your code since 2 hours and guess what. It still doesn't work for me, it will get me crazy... Here is my code : img11.hostingpics.net/pics/990905error1.png and here is the error message that i get in android studio : img11.hostingpics.net/pics/246513error2.png. In my opinion, it's due to the wrong library that i choose to work with, i should try the google one to fix this problem, what do you think please?
  • fujitsu4
    fujitsu4 over 8 years
    Can you please tell me what import have you used, please? I'm so so confused. I used some like these : import com.android.volley.RequestQueue or import com.android.volley.Response. So it's the google library and not the mcxiaoke one no????
  • fujitsu4
    fujitsu4 over 8 years
    YES, YES, YES, it finally works. Thank you so so so so so much, really THANK you man !
  • BNK
    BNK over 8 years
    Glad my answer could help you!
  • fujitsu4
    fujitsu4 over 8 years
    Can you tell me your email PLEASE? I just want to ask about an Android project (which is very very urgent because I have to finish it before this friday :( Thanks !
  • BNK
    BNK over 8 years
    My email [email protected] :)
  • Admin
    Admin over 8 years
    @BNK I saw your profile and I feel you might be able to help me the problem is I am sending some json data to mysql database using volley but the value of my last entry updates all the other values inside the database. This is a link to my code pastebin.com/DGVQkNJC
  • BNK
    BNK over 8 years
    @anup I cannot find where you call "insertToDb" inside your code. However, IMO, you should create a new question in S.O instead comments here, because perhaps there will be many other comments beside above ones.
  • ka_lin
    ka_lin over 7 years
    Exactly what I needed :)
  • SAndroidD
    SAndroidD over 7 years
    hi , I used your suggested, but it gives com.android.volley.NoConnectionError: java.net.ConnectException error.
  • BNK
    BNK over 7 years
    @SAndroidD check the connection from your phone to the web server/web service address, or you can search more in StackOverflow about that exception
  • SAndroidD
    SAndroidD over 7 years
    connection is fine .when I use 1.0 version it working fine,but when I use 1.0.17 version it throws following error 'com.android.volley.NoConnectionError'
  • BNK
    BNK over 7 years
    @SAndroidD you can try with 1.0.19 at github.com/mcxiaoke/android-volley, however, as his notice this project is deprecated and no longer being maintained...