how do i send Json request to a server- java project (NetBeans) -
i'm trying send json request server java project, problem is, found class wrote, in uses apache, tried make path apache still doesnt import stuff:
import org.apache.http.httpresponse; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; here's class:
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.net.malformedurlexception; import org.apache.http.httpresponse; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; public class jsonhttprequest { private static string posttourl(string url, string message, defaulthttpclient httpclient) throws ioexception, illegalstateexception, unsupportedencodingexception, runtimeexception { httppost postrequest = new httppost(url); stringentity input = new stringentity(message); input.setcontenttype("application/json"); postrequest.setentity(input); httpresponse response = httpclient.execute(postrequest); if (response.getstatusline().getstatuscode() != 200) { throw new runtimeexception("failed : http error code : " + response.getstatusline().getstatuscode()); } bufferedreader br = new bufferedreader( new inputstreamreader((response.getentity().getcontent()))); string output; stringbuffer totaloutput = new stringbuffer(); system.out.println("output server .... \n"); while ((output = br.readline()) != null) { system.out.println(output); totaloutput.append(output); } return totaloutput.tostring(); } }
apatche third party have include apatche lib take @ page
Comments
Post a Comment