1

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"));
user1684140
  • 444
  • 6
  • 18
  • 3
    I just want to note that `NameValuePair and BasicNameValuePair have been deprecated ` i recomand to learn something new – Moudiz Nov 10 '15 at 05:57
  • If you are using ArrayList, then can you please explain your output format in brief? – Ajay Nov 10 '15 at 06:14

4 Answers4

2

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);
Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35
  • If this answer helpful to you, please accept this answer as you have seen the tickmark below the vote, so more and more people will know about this answer. – Parth Bhayani Nov 10 '15 at 06:01
2

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();
}
Ajay
  • 1,189
  • 1
  • 12
  • 28
  • What are the alternative for HttpClasses, NameValuePair and BasicNameValuePair? you have a good example for simple post action? Thank you. – user1684140 Nov 10 '15 at 08:27
  • Use HttpURLConnection, this is the good ( best ) alternative for HttpClasses. – Ajay Nov 16 '15 at 06:41
  • @user1684140 : if you find this is helpful then don't forget to accept this answer. or if you need any further assistance about api call or Json parsing, add/post your comment – Ajay Nov 16 '15 at 09:23
0

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"));
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

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 :)

Nouran S. Ahmad
  • 503
  • 1
  • 5
  • 19