Tags
Hi, i search long time in internet and i found various method to parse SOAP service.
At Create Log-in Activity (This are the Ui Activity)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* @author admin
*
*/
public class LoginActivity extends Activity {
private EditText loginPhoneNumberEditText;
private EditText loginPasswordEditText;
private Button submitButton;
private Button cancelButton;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
loginPhoneNumberEditText = (EditText) findViewById(R.id.loginphonenumberEditText);
loginPasswordEditText = (EditText) findViewById(R.id.loginpasswordEditText);
submitButton = (Button) findViewById(R.id.loginsubmitButton);
cancelButton = (Button) findViewById(R.id.logincancelButton);
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new LoginResponse(LoginActivity.this, loginPhoneNumberEditText
.getText().toString(), loginPasswordEditText.getText()
.toString()).execute();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent home = new Intent(Intent.ACTION_MAIN);
home.addCategory(Intent.CATEGORY_HOME);
home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(home);
}
});
}
}
Next Step to create a Class the that Post HTTPPOST Request
public class WebService {
private String SOAPRequestXMLHeader = “<?xml version=’1.0′ encoding=’utf-8′?><soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ xmlns:xsd=’http://www.w3.org/2001/XMLSchema’ xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>”;
private String SOAPRequestXMLEnd = “</soap:Envelope>”;
public String Login(String URL, String Method, String key1,String key2, String phonenumber, String password) {
String SOAPRequestXMLBody = “<soap:Body><” + Method
+ ” xmlns=’http://tempuri.org/’><strKey1>” + key1
+ “</strKey1><strKey2>” + key2
+ “</strKey2><strGSMNo>” + phonenumber
+ “</strGSMNo><strPassword>” + password
+ “</strPassword></Login></soap:Body>”;
String soapAction = “http://tempuri.org/” + Method;
String SOAPRequestXML = SOAPRequestXMLHeader + SOAPRequestXMLBody
+ SOAPRequestXMLEnd;
return CallWebService(URL, soapAction, SOAPRequestXML);
}
public String CallWebService(String url, String soapAction, String envelope) {
final DefaultHttpClient httpClient = new DefaultHttpClient();
// request parameters
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 15000);
// set parameter
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
// POST the envelope
HttpPost httppost = new HttpPost(url);
// add headers
httppost.setHeader(“soapaction”, soapAction);
httppost.setHeader(“Content-Type”, “text/xml; charset=utf-8”);
String responseString = “”;
try {
// the entity holds the request
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
// Response handler
ResponseHandler<String> rh = new ResponseHandler<String>() {
// invoked when client receives response
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// get response entity
HttpEntity entity = response.getEntity();
// read the response as byte array
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
// write the response byte array to a string buffer
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString = httpClient.execute(httppost, rh);
} catch (Exception e) {
Log.v(“exception”, e.toString());
}
// close the connection
httpClient.getConnectionManager().shutdown();
Log.i(“Webservice”, responseString);
return responseString;
}
}
After that , We need to parse Return XML.
public class WebServiceParser {
public int getLoginResponseCode(String response) throws Exception {
Node node = getLoginNode(response);
return Integer.parseInt(node.getTextContent());
}
private Node getLoginNode(String response) throws Exception {
Document doc = getXMLFromString(response);
NodeList authTokenNodeList = doc
.getElementsByTagName(“LoginResult”);
return authTokenNodeList.item(0);
}
private Document getXMLFromString(String xml) throws Exception {
Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xml)));
return doc;
}
}