I have an API which I want to use to fetch data. For fetching data I have to send a request in XML format and response will be sent in XML. Does anyone have any example how to send a request using Java and how to decode the response in java.
Asked
Active
Viewed 5,887 times
1
-
1Your question is too vague as for me... Are you talking about web services? What API is based on? Maybe its just an http connection to soe servlet? Please elaborate a little – Mark Bramnik Oct 01 '12 at 17:16
-
1Could you be a little more specific on how you want to send a request via XML? It can be done by POST, SOAP, AIM SIM etc... And to what - web service, web page.. Checkout the javax.xml.soap package – Ben Sewards Oct 01 '12 at 17:17
-
@BenSewards POST ..I have a URL for the application and it accepts POST requests in XML format. – yogsma Oct 01 '12 at 17:20
-
@yogsma you can use the OutputStreamWriter for sending a POST requesting using a URL: http://www.exampledepot.com/egs/java.net/post.html – Ben Sewards Oct 01 '12 at 17:25
2 Answers
2
Well i have just what you wanted... but i would ask you to use the following APIs ...
JAXP
andJAXB
Castor
- The below code snippet method accepts the url
of the web-server and the xmlQuery
- I have used the NameValuePair
to send the XML request
- Please replace the MySSLSocketFactory.getNewHttpClient();
with an Http
Client, i have used this it needs a custom certificate to access this site.`
Here is the code from my Project, that sends an XML req and gets back an XML resp :
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = MySSLSocketFactory.getNewHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now", sb + "");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method " + sb.toString());
return sb.toString();
}

Kumar Vivek Mitra
- 33,294
- 6
- 48
- 75
0
Have a look at the following discussion, How to send HTTP request in java? For the response in xml, make sure that the mime-type is set to application/xml. Hope this answers your question.