0
I want to invoke my live webservice on android activity and i am using Ksoap technique ,I have made activity are as follows;
Webservice live on server as -'http://scoolbag.somee.com/service.asmx'
Method Name- 'TalkTalk'
Working EmpId- 837382
I have made two java files out of which one is activity java file and by other i am calling my webservice.
namely 'Webservice.java' for ANDROID ACTIVITY
'WebserviceCall.java'for CALLING WEBSERVICE
==================================================================
现在,我会引导到我的代码段,我与名“webservice.java”如何从Android的活动调用点净web服务与KSOAP
Android的活动
public class webservice extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webservice);
final Button webserviceCallButton = (Button) findViewById(R.id.btnInvoke);
final EditText webserviceResponse = (EditText) findViewById(R.id.editEID);
webserviceCallButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Requesting to server",
Toast.LENGTH_LONG).show();
//Create Webservice class object
WebserviceCall com = new WebserviceCall();
// Initialize variables
String Eid= webserviceResponse.getText().toString();
//Call Webservice class method and pass values and get response
String aResponse = com.getTalkTalk(Eid);
//Alert message to show webservice response
Toast.makeText(getApplicationContext(), Eid+" User= "+aResponse+" Name",
Toast.LENGTH_LONG).show();
Log.i("AndroidExampleOutput", "----"+aResponse);
webserviceResponse.setText("Response : "+aResponse);
}
});
}}
活动
现在我将向您展示我在调用webservice的代码并注意您已将具有依赖关系的Ksaop Jar文件插入到liberary文件夹中。请看下面提到的代码。
WebserviceCall.java
public class WebserviceCall {
String namespace = "http://www.niceald.in/";
private String url = "http://scoolbag.somee.com/service.asmx";
String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
AndroidHttpTransport androidHttpTransport;
WebserviceCall() {
}
/**
* Set Envelope
*/
protected void SetEnvelope() {
try {
// Creating SOAP envelope
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//You can comment that line if your web service is not .NET one.
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
androidHttpTransport = new AndroidHttpTransport(url);
androidHttpTransport.debug = true;
} catch (Exception e) {
System.out.println("Soap Exception---->>>" + e.toString());
}
}
// MethodName variable is define for which webservice function will call
public String getTalkTalk(String EMPid)
{
try {
SOAP_ACTION = namespace + EMPid;
//Adding values to request object
request = new SoapObject(namespace, EMPid);
//Adding Double value to request object
// PropertyInfo weightProp =new PropertyInfo();
SetEnvelope();
try {
//SOAP calling webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
//Got Webservice response
String result = envelope.getResponse().toString();
return result;
} catch (Exception e) {
// TODO: handle exception
return e.toString();
}
} catch (Exception e) {
// TODO: handle exception
return e.toString();
}
}}
我得到一个异常NetworkonMainThread,请仔细阅读我的代码并用其他的帮助。最好的方法是使用我的web服务并给你的代码作为答案。
'Thanks In Advance'
虽然这在理论上可以回答的问题,[这将是优选的](// meta.stackoverflow.com/q/8259)以包括回答的主要部分在这里,与提供链接供参考。 – Tunaki
虽然这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/11029078) –
更新了我的答案。 @AhmedAshour –