Parse XML using XmlPullParser.
Ksoap library essentially required to parse XML, which will be in libs folder of project.
Initially we have to create parser Handler class based on xml response from server.
AllHuudleHandler .java
public class AllHuudleHandler extends DefaultHandler {
private List<AllHuudleModel> huudleList; private String text;
private AllHuudleModel huudle;
private StringBuffer stringBuffer=null;
public AllHuudleHandler() {
huudleList = new ArrayList<AllHuudleModel>();
stringBuffer = new StringBuffer();
}
public List<AllHuudleModel> getHuudleList() {
return huudleList;
}
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
text="";
if (qName.equalsIgnoreCase("GroupHuudle")) {
// create a new instance of employee
huudle = new AllHuudleModel();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
// text = new String(ch, start, length);
stringBuffer.append(new String(ch, start, length));
}
public void endElement(String uri, String localName, String tagname)
throws SAXException {
text = stringBuffer.toString();
stringBuffer.setLength(0);
if (tagname.equalsIgnoreCase("GroupHuudle")) {
huudleList.add(huudle);
// add it to the list
}
else if (tagname.equalsIgnoreCase("Id")) {
huudle.setId(text);
} else if (tagname.equalsIgnoreCase("ProfileId")) {
huudle.setProfileId(text);
} else if (tagname.equalsIgnoreCase("GroupHandle")) {
huudle.setGroupHandle(text);
} else if (tagname.equalsIgnoreCase("GroupName")) {
huudle.setGroupName(text);
} else if (tagname.equalsIgnoreCase("DateCreated")) {
huudle.setDateCreated(text);
} else if (tagname.equalsIgnoreCase("IsEmailVerified")) {
huudle.setIsEmailVerified(text);
}
}
HTTP class which will make request and return the response in String:
HTTPOST.java
public class HTTPOST {
public String getResponseByXML(String URL, String request) {
HttpPost httpPost = new HttpPost(URL);
StringEntity entity;
String response_string = null;
try {
entity = new StringEntity(request, HTTP.UTF_8);
httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
// httpPost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
response_string = EntityUtils.toString(response.getEntity());
Log.d("request", response_string);
} catch (Exception e) {
e.printStackTrace();
}
return response_string;
}
getResponseByFile Function:
public String getResponseByFile(String URL) {
HttpPost httpPost = new HttpPost(URL); FileEntity entity;
String response_string = null;
try {
entity = new FileEntity(new File(Environment.getExternalStorageDirectory()+File.separator+"request.xml"), "UTF-8");
httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
// httpPost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
response_string = EntityUtils.toString(response.getEntity());
Log.d("request", response_string);
} catch (Exception e) {
e.printStackTrace();
}
return response_string;
}
}
and finally this is the way to create xml request and get the response, prefer AsynTask to make request and response:
Code:
String xml = null;
xml = HuudleConstants.convertStreamToString(getActivity()
.getAssets().open("Test.xml")); // Name of the XML which we kept inside assets folder
String request = String.format(xml, _StrToken, huudleId,
_StruserId);
HTTPOST httpost = new HTTPOST();
response = httpost.getResponseByXML(HuudleConstants.URL,
request);
Log.e("@@@", "response=" + response);
XML Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="here will be api url">
<soapenv:Header/>
<soapenv:Body>
<api:UpdateUserPassword>
<!--Optional:-->
<api:token>%s</api:token>
<!--Optional:-->
<api:userid>%s</api:userid>
<!--Optional:-->
<api:oldPassword>%s</api:oldPassword>
<!--Optional:-->
<api:newPassword>%s</api:newPassword>
</api:UpdateUserPassword>
</soapenv:Body>
</soapenv:Envelope>
Ksoap library essentially required to parse XML, which will be in libs folder of project.
Initially we have to create parser Handler class based on xml response from server.
AllHuudleHandler .java
public class AllHuudleHandler extends DefaultHandler {
private List<AllHuudleModel> huudleList; private String text;
private AllHuudleModel huudle;
private StringBuffer stringBuffer=null;
public AllHuudleHandler() {
huudleList = new ArrayList<AllHuudleModel>();
stringBuffer = new StringBuffer();
}
public List<AllHuudleModel> getHuudleList() {
return huudleList;
}
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
text="";
if (qName.equalsIgnoreCase("GroupHuudle")) {
// create a new instance of employee
huudle = new AllHuudleModel();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
// text = new String(ch, start, length);
stringBuffer.append(new String(ch, start, length));
}
public void endElement(String uri, String localName, String tagname)
throws SAXException {
text = stringBuffer.toString();
stringBuffer.setLength(0);
if (tagname.equalsIgnoreCase("GroupHuudle")) {
huudleList.add(huudle);
// add it to the list
}
else if (tagname.equalsIgnoreCase("Id")) {
huudle.setId(text);
} else if (tagname.equalsIgnoreCase("ProfileId")) {
huudle.setProfileId(text);
} else if (tagname.equalsIgnoreCase("GroupHandle")) {
huudle.setGroupHandle(text);
} else if (tagname.equalsIgnoreCase("GroupName")) {
huudle.setGroupName(text);
} else if (tagname.equalsIgnoreCase("DateCreated")) {
huudle.setDateCreated(text);
} else if (tagname.equalsIgnoreCase("IsEmailVerified")) {
huudle.setIsEmailVerified(text);
}
}
HTTP class which will make request and return the response in String:
HTTPOST.java
public class HTTPOST {
public String getResponseByXML(String URL, String request) {
HttpPost httpPost = new HttpPost(URL);
StringEntity entity;
String response_string = null;
try {
entity = new StringEntity(request, HTTP.UTF_8);
httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
// httpPost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
response_string = EntityUtils.toString(response.getEntity());
Log.d("request", response_string);
} catch (Exception e) {
e.printStackTrace();
}
return response_string;
}
getResponseByFile Function:
public String getResponseByFile(String URL) {
HttpPost httpPost = new HttpPost(URL); FileEntity entity;
String response_string = null;
try {
entity = new FileEntity(new File(Environment.getExternalStorageDirectory()+File.separator+"request.xml"), "UTF-8");
httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
// httpPost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
response_string = EntityUtils.toString(response.getEntity());
Log.d("request", response_string);
} catch (Exception e) {
e.printStackTrace();
}
return response_string;
}
}
and finally this is the way to create xml request and get the response, prefer AsynTask to make request and response:
Code:
String xml = null;
xml = HuudleConstants.convertStreamToString(getActivity()
.getAssets().open("Test.xml")); // Name of the XML which we kept inside assets folder
String request = String.format(xml, _StrToken, huudleId,
_StruserId);
HTTPOST httpost = new HTTPOST();
response = httpost.getResponseByXML(HuudleConstants.URL,
request);
Log.e("@@@", "response=" + response);
XML Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="here will be api url">
<soapenv:Header/>
<soapenv:Body>
<api:UpdateUserPassword>
<!--Optional:-->
<api:token>%s</api:token>
<!--Optional:-->
<api:userid>%s</api:userid>
<!--Optional:-->
<api:oldPassword>%s</api:oldPassword>
<!--Optional:-->
<api:newPassword>%s</api:newPassword>
</api:UpdateUserPassword>
</soapenv:Body>
</soapenv:Envelope>
Comments
Post a Comment