os android 4.0.3,要使用android 4.0.3中的教程,如果必须使用As...

我使用了教程http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/,并为机器人4.0.3做了一些机会!

但它仍然没有!

有人能给我一个正确的解决方案吗?

package com.webservice;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;

import android.graphics.Bitmap;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class FirstScreen extends Activity

{

/** Called when the activity is first created. */

private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";

private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";

private static String NAMESPACE = "http://tempuri.org/";

private static String METHOD_NAME1 = "FahrenheitToCelsius";

private static String METHOD_NAME2 = "CelsiusToFahrenheit";

private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

Button btnFar,btnCel,btnClear;

EditText txtFar,txtCel;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_first_screen);

btnFar = (Button)findViewById(R.id.btnFar);

btnCel = (Button)findViewById(R.id.btnCel);

btnClear = (Button)findViewById(R.id.btnClear);

txtFar = (EditText)findViewById(R.id.txtFar);

txtCel = (EditText)findViewById(R.id.txtCel);

btnFar.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

new one1().execute();

}

});

btnCel.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

new one2().execute();

}

});

btnClear.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

txtCel.setText("");

txtFar.setText("");

}

});

}

private class one1 extends AsyncTask {

@Override

protected String doInBackground(String... arg0) {

// TODO Auto-generated method stub

//Initialize soap request + add parameters

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

//Use this to add parameters

request.addProperty("Fahrenheit",txtFar.getText().toString());

//Declare the version of the SOAP request

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);

envelope.dotNet = true;

try {

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

//this is the actual part that will call the webservice

androidHttpTransport.call(SOAP_ACTION1, envelope);

// Get the SoapResult from the envelope body.

SoapObject result = (SoapObject)envelope.bodyIn;

if(result != null)

{

//Get the first property and change the label text

txtCel.setText(result.getProperty(0).toString());

}

else

{

Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

private class one2 extends AsyncTask {

@Override

protected String doInBackground(String... params) {

// TODO Auto-generated method stub

//Initialize soap request + add parameters

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);

//Use this to add parameters

request.addProperty("Celsius",txtCel.getText().toString());

//Declare the version of the SOAP request

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);

envelope.dotNet = true;

try {

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

//this is the actual part that will call the webservice

androidHttpTransport.call(SOAP_ACTION2, envelope);

// Get the SoapResult from the envelope body.

SoapObject result = (SoapObject)envelope.bodyIn;

if(result != null)

{

//Get the first property and change the label text

txtFar.setText(result.getProperty(0).toString());

}

else

{

Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

}

} catch (Exception e) {

e.printStackTrace();

} return null;

}

}

}

错误日志和代码

private class one1 extends AsyncTask {

protected SoapObject doInBackground(String... arg0) {

protected **void** onPreExecute(**)**

{

//value from textview/edittext

}

// TODO Auto-generated method stub

//Initialize soap request + add parameters

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

//Use this to add parameters

request.addProperty("Fahrenheit","30");// provide value here

//Declare the version of the SOAP request

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);

envelope.dotNet = true;

try {

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

androidHttpTransport.call(SOAP_ACTION1, envelope);

**result** = (SoapObject)envelope.bodyIn;

} catch (Exception e) {

e.printStackTrace();

}

return **result**;

}

protected void onPostExecute(SoapObject result)

{

if(result != null)

{

System.out.println(result.getProperty(0).toString());

}

else

{

Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

}

}

}

}

语法错误,插入“enum Identifier”以完成EnumHeader

令牌“void”,@ expected上的语法错误

结果无法解析为变量FirstScreen.java

结果无法解析为变量FirstScreen.java

编辑代码:

package com.webservice;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;

import android.app.ProgressDialog;

import android.view.View;

import android.view.View.OnClickListener;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class FirstScreen extends Activity

{

private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";

private static String NAMESPACE = "http://tempuri.org/";

private static String METHOD_NAME1 = "FahrenheitToCelsius";

private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

SoapObject result;

Button b;

EditText et;

int value;

TextView tv;

ProgressDialog pd;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

pd= new ProgressDialog(this);

pd.setTitle("Making Soap Request");

setContentView(R.layout.activity_first_screen);

b= (Button) findViewById(R.id.button1);

tv= (TextView) findViewById(R.id.textView2);

et= (EditText) findViewById(R.id.ed);

b.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

value=Integer.parseInt(et.getText().toString());

new one1().execute();

}

});

}

private class one1 extends AsyncTask {

protected void onPreExecute()

{

pd.show();

}

protected SoapObject doInBackground(String... arg0) {

// TODO Auto-generated method stub

//Initialize soap request + add parameters

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

//Use this to add parameters

request.addProperty("Fahrenheit",value);

//Declare the version of the SOAP request

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);

envelope.dotNet = true;

try {

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

androidHttpTransport.call(SOAP_ACTION1, envelope);

result = (SoapObject)envelope.bodyIn;

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

protected void onPostExecute(SoapObject result)

{

pd.dismiss();

if(result != null)

{

tv.setText("Result = "+result.getProperty(0).toString());

}

else

{

Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值