creating complex JSON using BasicNameValuePair

3.5k Views Asked by At

How can I create this JSON using ArrayList for posting json in Android

{
"user" : 
    {
        "nickname" : "nickname6",
        "password" : "1234",
    }
}

I get only to a flat JSON

    ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("nickname","nickname6"));
    nameValuePairs.add(new BasicNameValuePair("password", "1234"));
4

There are 4 best solutions below

1
Parth Bhayani On

You need to create a JSON, and for that you have to add that JSON as a parameter in your POST method. To do that you can try this :

JSONObject json = new JSONObject(jsonString);
JSONObject joParams = json.getJSONObject("params");
String nickname = joParams.getString("nickname");

    post.add(new BasicNameValuePair("nickname", nickname);
3
Ajay On

As you know, HttpClasses, NameValuePair and BasicNameValuePair have been deprecated in latest android. we should avoid it now.

and If you want to create

{
   "user":{
        "nickname":"nickname6"
        "password":"1234",             
    }
}

Than you can use below code sample to create the same json using JSONObject class.

JSONObject jObj = new JSONObject();

try {       
    JSONObject userCredentials = new JSONObject();
    userCredentials.put("nickname","nickname6");
    userCredentials.put("password","1234");

    jObj.put("user", userCredentials);
} catch(Exception e) {
   e.printStackTrace();
}
0
Aditya Vyas-Lakhan On

try to use ContentValues like this way

ContentValues values=new ContentValues();
values.put("username",name);
values.put("password",password);

OR Use MultipartEntity

MultipartEntity multi = new MultipartEntity();
multi.addPart("name", new StringBody("your data"));
multi.addPart("Id", new StringBody("123"));
0
Nouran S. Ahmad On

here's an example for http post using AsyncTask:

public class httpSendrequest extends AsyncTask<Void,Void,String> {

        ArrayList<NameValuePair> nvPairs=new ArrayList<>();

    Boolean error=false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //here you initialize your json object and add it to value pairs
            JSONObject jObj = new JSONObject();

    try {       
            JSONObject userCredentials = new JSONObject();
            userCredentials.put("nickname","nickname6");
            userCredentials.put("password","1234");

            jObj.put("user", userCredentials);
    nvPairs.add(new BasicNameValuePair("jsonstring",jObj.toString()));
    } catch(Exception e) {
      error=true;
    }

        }

        @Override
        protected String doInBackground(Void... params) {

    if(!error)
            try{

                String link ="http://"+ip+"/QSystem.asmx/insert_answer" ;
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(link); //adding URL to the http post
                httppost.setEntity(new UrlEncodedFormEntity(nvPairs, HTTP.UTF_8)); //adding the value pairs and encoding to the http post request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();


            } catch (Exception e){
                error=true;
            }

            return "str";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if(!error) {
                Toast.makeText(getApplicationContext(), "Sent", Toast.LENGTH_SHORT).show();

            }
        }

    }

then you can call it from onCreate or in an OnClickListener for a button, like this:

new httpSendrequest().execute();

hope this helps :)