Write json content to url android -


i have android requirement of parsing json content url , have completed them, need send results json content server url. searched many previous posts, specify how download , parse json content. inputs/examples of how start off posting contents url in json of immense help!

edit: below sample code attached of trying

try {          url url = new url("http://localhost:8080/restfulexample/json/product/post");         httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setdooutput(true);         conn.setrequestmethod("post");         conn.setrequestproperty("content-type", "application/json");          string input = "{\"qty\":100,\"name\":\"ipad 4\"}";          outputstream os = conn.getoutputstream();         os.write(input.getbytes());         os.flush();          if (conn.getresponsecode() != httpurlconnection.http_created) {             throw new runtimeexception("failed : http error code : "                     + conn.getresponsecode());         }          bufferedreader br = new bufferedreader(new inputstreamreader(                 (conn.getinputstream())));          string output;         system.out.println("output server .... \n");         while ((output = br.readline()) != null) {             system.out.println(output);         }          conn.disconnect();      } catch (malformedurlexception e) {          e.printstacktrace();      } catch (ioexception e) {          e.printstacktrace();      } 

on executing this, getting connection refused, java.net.connect exception! please help!!

you can use below method sending json request.

public static httpresponse sendrequest(string url, string request) throws exception  {     //create httpclient make request     defaulthttpclient httpclient = new defaulthttpclient();      //create httppost request object     httppost httpost = new httppost(url);      //create string entity passed httppost request     stringentity se = new stringentity(request));      //set created stringentity     httpost.setentity(se);      //the intended     httpost.setheader("accept", "application/json");     httpost.setheader("content-type", "application/json");      return httpclient.execute(httpost); } 

hope helps.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -