json - How to handle android post request -


i have send request in following json format

  {     "expert_request":     {      "topic":"gggg",      "expert":"10"     }    } 

i used following code not working. have set topic , expert field. how set expert_request field?

protected jsonobject doinbackground(string... params) {             // todo auto-generated method stub             string returnvalue = null;              string topic = params[0]; // topic             string doctor_id = params[1]; // expert              // building parameters             list<namevaluepair> paramlist = new arraylist<namevaluepair>();             paramlist.add(new basicnamevaluepair("topic", topic));             paramlist.add(new basicnamevaluepair("expert", doctor_id));              jsonobject json = jparser.makehttprequest(url_request, "post", paramlist);              return json;         }   public jsonobject makehttprequest(string url, string method,             list<namevaluepair> params) {          log.e("url", url);          // making http request         try {              // check request method             if(method == "post"){                 // request method post                 // defaulthttpclient                 defaulthttpclient httpclient = new defaulthttpclient();                 httppost httppost = new httppost(url);                 httppost.setentity(new urlencodedformentity(params));                  httpresponse httpresponse = httpclient.execute(httppost);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();              }else if(method == "get"){                 // request method                 defaulthttpclient httpclient = new defaulthttpclient();                  if(params.size() > 0){                     string paramstring = urlencodedutils.format(params, "utf-8");                     url += "?" + paramstring;                 }                    httpget httpget = new httpget(url);                  httpresponse httpresponse = httpclient.execute(httpget);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();             }                     } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          try {             bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "iso-8859-1"), 8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());         }          // try parse string json object         try {             jobj = new jsonobject(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.tostring());         }          // return json string         return jobj;      } 

why not this:

public jsonobject makejson(string topic, string expert) {     jsonobject parent = new jsonobject();     jsonobject child = new jsonobject();     child.put("topic", topic);     child.put("expert", expert);     parent.put("expert_request", child);     return parent; } 

that should formatted json object. sending request, might want take @ volley library google. works lot nicer asynctask.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -