Skip to main content
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>


Comments

Popular posts from this blog

Getview Method Wierd in Listview

Its common issue in android and most of newbee faced. To understand why its happening, you have to read about behavior of Getview method of listivew, its your home work, let continue with tutorial. Below tutorial will explain that how to maintain the position of button  which is in row file using settag and gettag. main.xml ( its contain listview which will hold row data) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:descendantFocusability="blocksDescendants">  <ListView         android:id="@+id/listView"         android:layout_width="fill_parent"         android:layout_height="fill_parent"/> </LinearLayout> ...

Amazon S3 Configuration

Gradles //amazon s3 compile 'com.amazonaws:aws-android-sdk-core:2.2.+' compile 'com.amazonaws:aws-android-sdk-s3:2.2.+' Declare in manefiest file <!--AMAZON S3--> < service android :name= "com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android :enabled= "true" /> AmazonUtils.java import android.content.Context; import android.net.Uri; import com.amazonaws.auth.CognitoCachingCredentialsProvider; import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver; import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3Client; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.UUID; /* * Handles basic helper functions used throughout the app. */ public class AmazonUtil { // We only n...
Update Progress in notification: public class NetworkService extends IntentService {     public static final String FILE_PATH = "file_path";     public static final String _FILE_PATH = "_path";     public static final String PERCENT_EXTRA = "percent";     public static final String FILE_NAME_EXTRA = "FileName";     public static final String STATE_EXTRA = "state";     public static final String IS_ALBUM_UPLAODING = "IsAlbumUplaoding";     public static final String XML = "xml";     public static final String TOKEN = "token";     public static final String MESSAGE = "mesasge";     public static final String POST_DATE = "postdate";     public static final String USERID = "userid";     public static final String GROUPID = "groupid";     public static final String PATH = "path"; ...