Skip to content

Instantly share code, notes, and snippets.

@worker8
Created April 1, 2015 02:52
Show Gist options
  • Select an option

  • Save worker8/20eb5bd3400b88f10973 to your computer and use it in GitHub Desktop.

Select an option

Save worker8/20eb5bd3400b88f10973 to your computer and use it in GitHub Desktop.
Example of POST method to call webservice with Volley
public class PriceConfirmationTask {
private Callback mCallback;
String mDeliveryTime;
Activity mActivity;
public interface Callback{
public void onResponse(PriceCheckResult result);
}
public PriceConfirmationTask(Activity activity, String deliveryTime, Callback callback) {
mActivity = activity;
mDeliveryTime = deliveryTime;
mCallback = callback;
}
public PriceCheckResult parseXML(String input){
{
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = null;
xpp = factory.newPullParser();
xpp.setInput(new StringReader(input));
int eventType = 0;
eventType = xpp.getEventType();
String currentTag="";
Log.d("ddw","input: "+input);
int foodMainItemPosition = 0;
int extraItemPosition = 0;
int miscChargesPosition = 0;
PriceCheckResult priceCheckResult = new PriceCheckResult();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
System.out.println("End document");
} else if(eventType == XmlPullParser.START_TAG) {
currentTag = xpp.getName();
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
currentTag = xpp.getName();
System.out.println("End tag "+xpp.getName());
if (currentTag.equals("FoodMainItem")){
foodMainItemPosition++;
extraItemPosition = 0;
} else if (currentTag.equals("MiscChargess")){
miscChargesPosition++;
}
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("tag, Text "+currentTag+","+xpp.getText());
if (currentTag.equals("CustomerID")){
priceCheckResult.CustomerID = xpp.getText();
} else if (currentTag.equals("Email")){
priceCheckResult.Email = xpp.getText();
} else if (currentTag.equals("Contact")){
priceCheckResult.Contact = xpp.getText();
} else if (currentTag.equals("RestaurantID")){
priceCheckResult.RestaurantID = xpp.getText();
} else if (currentTag.equals("TimePickup")){
priceCheckResult.TimePickup = xpp.getText();
} else if (currentTag.equals("VoucherCode")){
priceCheckResult.VoucherCode = xpp.getText();
} else if (currentTag.equals("product_id")){
if (priceCheckResult.foodItems.size() == foodMainItemPosition){
priceCheckResult.foodItems.add(new FoodMainItem());
}
priceCheckResult.foodItems.get(foodMainItemPosition).product_id = xpp.getText();
} else if (currentTag.equals("qty")){
if (priceCheckResult.foodItems.size() == foodMainItemPosition){
priceCheckResult.foodItems.add(new FoodMainItem());
}
priceCheckResult.foodItems.get(foodMainItemPosition).qty = xpp.getText();
} else if (currentTag.equals("name")){
if (priceCheckResult.foodItems.size() == foodMainItemPosition){
priceCheckResult.foodItems.add(new FoodMainItem());
}
priceCheckResult.foodItems.get(foodMainItemPosition).name = xpp.getText();
} else if (currentTag.equals("unit_price")){
if (priceCheckResult.foodItems.size() == foodMainItemPosition){
priceCheckResult.foodItems.add(new FoodMainItem());
}
priceCheckResult.foodItems.get(foodMainItemPosition).unit_price = xpp.getText();
} else if (currentTag.equals("price")){
if (priceCheckResult.foodItems.size() == foodMainItemPosition){
priceCheckResult.foodItems.add(new FoodMainItem());
}
priceCheckResult.foodItems.get(foodMainItemPosition).price = xpp.getText();
} else if (currentTag.equals("ChargeDesc")){
if (priceCheckResult.MiscChargess.size() == miscChargesPosition){
priceCheckResult.MiscChargess.add(new MiscCharge());
}
priceCheckResult.MiscChargess.get(miscChargesPosition).ChargeDesc = xpp.getText();
} else if (currentTag.equals("ChargeAmount")){
if (priceCheckResult.MiscChargess.size() == miscChargesPosition){
priceCheckResult.MiscChargess.add(new MiscCharge());
}
priceCheckResult.MiscChargess.get(miscChargesPosition).ChargeAmount = xpp.getText();
} else if (currentTag.equals("TotalCharges")){
priceCheckResult.TotalCharges = xpp.getText();
}
}
eventType = xpp.next();
}
Log.d("ddw","priceCheckResult: "+priceCheckResult);
return priceCheckResult;
} catch (XmlPullParserException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
public void callEndpoint() {
try {
final StringRequest stringRequest = new StringRequest(Request.Method.POST, ApiConstant.KSOAP_BASE_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("ddw", "PopulateCart response: " + response.toString());
Log.d("ddw", "parseXML(response): " + response.toString());
mCallback.onResponse(parseXML(response));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("ddw", "Error: " + error.getMessage());
Log.d("ddw", ""+error.getMessage()+","+error.toString());
}
}){
@Override
public Map<String, String> getParams(){
return null;
}
@Override
public byte[] getBody(){
List<Menu> foods = Menu.listAll(Menu.class);
String foodItemString = "";
// Make food requests
for(Menu food:foods){
foodItemString+=" <ns1:FoodItems>\n" +
" <ns1:FoodMainItem>\n" +
" <ns1:product_id>"+food.getProduct_id()+"</ns1:product_id>\n" +
" <ns1:qty>"+food.getQuantity()+"</ns1:qty>\n" +
" </ns1:FoodMainItem>\n" +
" </ns1:FoodItems>\n";
}
// Log.d("ddw","foodItemString: "+foodItemString);
String temp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://tempuri.org/\">\n" +
" <SOAP-ENV:Body>\n" +
" <ns1:PopulateCart>\n" +
" <ns1:sc>\n" +
" <ns1:CustomerID>"+ Common.getSharedPrefValue(mActivity, Common.PREF_TOKEN_ID)+"</ns1:CustomerID>\n" +
" <ns1:Email>"+ Common.getSharedPrefValue(mActivity, Common.PREF_EMAIL)+"</ns1:Email>\n" +
" <ns1:Contact></ns1:Contact>\n" +
" <ns1:RestaurantID>"+ Restaurant.listAll(Restaurant.class).get(0).getRestaurant_id()+"</ns1:RestaurantID>\n" +
" <ns1:TimePickup>"+mDeliveryTime+"</ns1:TimePickup>\n" +
" <ns1:VoucherCode></ns1:VoucherCode>\n" +
foodItemString+
" </ns1:sc>\n" +
" </ns1:PopulateCart>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>\n";
// byte[] b = temp.getBytes();
Log.d("ddw","request String: "+temp);
byte[] b = temp.getBytes(Charset.forName("UTF-8"));
return b;
}
@Override
public String getBodyContentType()
{
return "text/xml;charset=utf-8";
}
};
new Thread(new Runnable() {
public void run() {
RequestQueue queue = Volley.newRequestQueue(mActivity);
queue.add(stringRequest);
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@ThavamSelvam

Copy link
Copy Markdown

hi
this code is not working for me
responce is 500 so how to parse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment