001.
package
com.myapp;
002.
003.
004.
import
java.io.IOException;
005.
006.
import
org.json.JSONException;
007.
import
org.json.JSONObject;
008.
import
org.ksoap2.SoapEnvelope;
009.
import
org.ksoap2.serialization.SoapObject;
010.
import
org.ksoap2.serialization.SoapSerializationEnvelope;
011.
import
org.ksoap2.transport.HttpTransportSE;
012.
import
org.xmlpull.v1.XmlPullParserException;
013.
014.
import
android.os.Bundle;
015.
import
android.os.StrictMode;
016.
import
android.view.Menu;
017.
import
android.view.View;
018.
import
android.widget.Button;
019.
import
android.widget.EditText;
020.
import
android.widget.TextView;
021.
import
android.widget.Toast;
022.
import
android.annotation.SuppressLint;
023.
import
android.app.Activity;
024.
025.
public
class
MainActivity
extends
Activity {
026.
030.
private
final
String METHOD_NAME =
"HelloWorld"
;
031.
032.
@SuppressLint
(
"NewApi"
)
033.
@Override
034.
public
void
onCreate(Bundle savedInstanceState) {
035.
super
.onCreate(savedInstanceState);
036.
setContentView(R.layout.activity_main);
037.
038.
039.
if
(android.os.Build.VERSION.SDK_INT >
9
) {
040.
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder()
041.
.permitAll().build();
042.
StrictMode.setThreadPolicy(policy);
043.
}
044.
045.
046.
Button btnSend = (Button)
this
.findViewById(R.id.btnSend);
047.
btnSend.setOnClickListener(
new
View.OnClickListener() {
048.
public
void
onClick(View v) {
049.
050.
051.
EditText txtName = (EditText) findViewById(R.id.txtName);
052.
053.
EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
054.
055.
056.
057.
TextView txtResultName = (TextView) findViewById(R.id.txtResultName);
058.
059.
TextView txtResultEmail = (TextView) findViewById(R.id.txtResultEmail);
060.
061.
062.
SoapObject request =
new
SoapObject(NAMESPACE, METHOD_NAME);
063.
request.addProperty(
"strName"
, txtName.getText().toString());
064.
request.addProperty(
"strEmail"
, txtEmail.getText().toString());
065.
066.
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(
067.
SoapEnvelope.VER11);
068.
069.
envelope.setOutputSoapObject(request);
070.
071.
HttpTransportSE androidHttpTransport =
new
HttpTransportSE(URL);
072.
073.
try
{
074.
075.
androidHttpTransport.call(SOAP_ACTION, envelope);
076.
SoapObject result = (SoapObject) envelope.bodyIn;
077.
078.
if
(result !=
null
) {
079.
080.
081.
082.
083.
084.
085.
JSONObject c =
new
JSONObject(result.getProperty(
0
).toString());
086.
String strResultName = c.getString(
"sName"
);
087.
String strResultEmail = c.getString(
"sEmail"
);
088.
txtResultName.setText(strResultName);
089.
txtResultEmail.setText(strResultEmail);
090.
091.
}
else
{
092.
Toast.makeText(getApplicationContext(),
093.
"Web Service not Response!"
, Toast.LENGTH_LONG)
094.
.show();
095.
}
096.
097.
}
catch
(IOException e) {
098.
099.
e.printStackTrace();
100.
}
catch
(XmlPullParserException e) {
101.
102.
e.printStackTrace();
103.
}
catch
(JSONException e) {
104.
105.
e.printStackTrace();
106.
}
107.
108.
}
109.
});
110.
111.
}
112.
113.
@Override
114.
public
boolean
onCreateOptionsMenu(Menu menu) {
115.
getMenuInflater().inflate(R.menu.activity_main, menu);
116.
return
true
;
117.
}
118.
119.
}