HTTPPOST with JSON Object having array -


below json object need pass on server:

{     "postimages": [{         "fileformat": "jpeg",         "id": "0",         "filebytearray": "long byte array of file",         "filecaption": "",         "videolink": ""     },{         "fileformat": "jpeg",         "id": "0",         "filebytearray": "long byte array of file",         "filecaption": "",         "videolink": ""     }],     "adminscheduleid": "0",     "categoryid": "1",     "userid": "4",     "presenter2": "0",     "ideatitle": "gfgfdgdfgdfg",     "isimplementedbyown": "false",     "noofvolunteer": "0",     "postid": "0",     "clubid": "2",     "description": " dfgdfgdf",     "presenter1": "0" } 

i have done passing json object server. create issue because of long byte array.

see below working code:

string res = null;      httppost request = new httppost(create_idea_url);     request.addheader("content-type", "application/json; charset=utf-8");     //request.addheader("content-type", "application/x-www-form-urlencoded");     try {          jsonarray array=new jsonarray(postimages); // array of images details          jsonobject jobject = new jsonobject()                     .put("ideatitle",ideatitle)                     .put("description",description)                     .put("isimplementedbyown",isimplementedbyown)                     .put("noofvolunteer",noofvolunteer)                     .put("postid",postid)                     .put("userid",userid)                     .put("clubid",clubid)                     .put("adminscheduleid",adminscheduleid)                     .put("categoryid",categoryid)                    .put("presenter1",presenter1)                    .put("presenter2",presenter2)                     .put("postimages",array);         stringentity entity = new stringentity(jobject.tostring(2),  "utf-8"); // add line have arabic language in post         system.out.println("jobject is:::: "+jobject.tostring());         request.setentity(entity);         system.out.println("====the entity is: "+entity.tostring());              defaulthttpclient httpclient = new defaulthttpclient();         //----------------------         if(lc5_api.getcookiesfromlc5() == null){         }else{          cookiestore cookiestore = new basiccookiestore();              for(cookie cook : lc5_api.getcookiesfromlc5()){                 cookiestore.addcookie(cook);              }             ((abstracthttpclient) httpclient).setcookiestore(cookiestore);         }             //-------------------         httpresponse response = httpclient.execute(request);         //res = response.tostring();         int rescode = response.getstatusline().getstatuscode();         system.out.println("our res: "+jobject.tostring());         system.out.println("result code: "+rescode);          bufferedreader in = new bufferedreader(new inputstreamreader(response.getentity().getcontent()));         string line="";         stringbuffer returnfromserver = new stringbuffer();         while ((line=in.readline())!=null){             returnfromserver.append(line);         }         //toast got server         //toast.maketext(getapplicationcontext(),returnfromserver.tostring(), toast.length_long).show();          system.out.println("responce: "+returnfromserver.tostring());         res = returnfromserver.tostring();          if (entity != null){             entity.consumecontent();         }         // toast.maketext(getapplicationcontext(),response.getstatusline().getstatuscode()+"", toast.length_long).show();     }catch(exception e){         e.printstacktrace();         res=null;     }      system.out.println("responcre is:"+res);     return res;*/  } public static string get_adoptidea(string postid) {     string responce = null;     restclient client = new restclient(get_adopt_idea);  //  if(lc5_api.debug) log.d("setcookie", lc5_api.session_cookiename + ":"+ lc5_api.session_cookievalue+""); //  client.addheader("cookie", session_cookiename+"="+lc5_api.session_cookievalue);      client.addparam("postid", postid);      try {         client.execute(requestmethod.get);         responce = client.getresponse();     } catch (connecttimeoutexception e) {         e.printstacktrace();         if(debug)log.d(tag, "");         responce=null;     }catch(exception e)     {         responce=null;     }      if(debug)log.d(tag, "response: "+responce);     return responce; 

above code works perfect. want send namevaluepair server.

see below code:

string res = null;     try {         httpclient client = new defaulthttpclient();           string url = create_idea_url;         httppost post = new httppost(url);          list<namevaluepair> postparameters;         postparameters = new arraylist<namevaluepair>();         //postparameters.add(new basicnamevaluepair("postid", postid));         postparameters.add(new basicnamevaluepair("ideatitle",ideatitle));         postparameters.add(new basicnamevaluepair("description",description));         postparameters.add(new basicnamevaluepair("isimplementedbyown",isimplementedbyown));         postparameters.add(new basicnamevaluepair("noofvolunteer",noofvolunteer));         postparameters.add(new basicnamevaluepair("postid",postid));         postparameters.add(new basicnamevaluepair("userid",userid));         postparameters.add(new basicnamevaluepair("clubid",clubid));         postparameters.add(new basicnamevaluepair("adminscheduleid",adminscheduleid));         postparameters.add(new basicnamevaluepair("categoryid",categoryid));         postparameters.add(new basicnamevaluepair("presenter1",presenter1));         postparameters.add(new basicnamevaluepair("presenter2",presenter2));           //postparameters.add(new basicnamevaluepair("postimages",ctx.getstring(r.string.json_object_test)));          jsonarray array = new jsonarray(postimages);          list<namevaluepair> param = new arraylist<namevaluepair>();            for(int i=0; i<array.length();i++){             imagesdata img = new imagesdata();               string video = array.getjsonobject(i).getstring("videolink").equalsignorecase("") ? "xyz" : array.getjsonobject(i).getstring("videolink");             system.out.println("video:: "+video);              param.add(new basicnamevaluepair("id", array.getjsonobject(i).getstring("id")));             param.add(new basicnamevaluepair("filecaption", array.getjsonobject(i).getstring("filecaption")));             param.add(new basicnamevaluepair("fileformat", array.getjsonobject(i).getstring("fileformat")));             param.add(new basicnamevaluepair("videolink", video));             param.add(new basicnamevaluepair("filebytearray", array.getjsonobject(i).getstring("filebytearray")));              postparameters.add(new basicnamevaluepair("postimages",param.tostring()));           }          post.setentity(new urlencodedformentity(postparameters));          post.setheader("content-type", "application/json; charset=utf-8");          if(lc5_api.debug) log.d("request rest:", "the requested string is:"+create_idea_url+postparameters);          //--------------------------------          if(lc5_api.getcookiesfromlc5() == null){          }else{             cookiestore cookiestore = new basiccookiestore();              //-------------------------------------------             for(cookie cook : lc5_api.getcookiesfromlc5()){                 cookiestore.addcookie(cook);              }             ((abstracthttpclient) client).setcookiestore(cookiestore);         }          //---------------------------------         httpresponse responsepost = client.execute(post);         httpentity resentitypost = responsepost.getentity();           if (resentitypost != null)          {             string response=entityutils.tostring(resentitypost);             res = response;         }     } catch (exception e) {e.printstacktrace();}     return res; 

now, when passes values above code gives me invalid json primitive: ideatitle.

everything fine not getting proper response. please me how pass json object having json array.

fyi, postimages json array details.

please me.


Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -