如果要在android 4.0.3中使用教程,必须使用AsynxTasc,但我仍然不工作?
我使用了android 4.0.3,并做了一些修改 但它仍然没有 有人能给我一个正确的解决方案吗如果要在android 4.0.3中使用教程,必须使用AsynxTasc,但我仍然不工作?,android,performance,soap,ksoap2,Android,Performance,Soap,Ksoap2,我使用了android 4.0.3,并做了一些修改 但它仍然没有 有人能给我一个正确的解决方案吗 package com.webservice; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportS
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<String, Integer, String> {
@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<String, Integer, String> {
@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;
}
}
}
package.com.webservice;
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.HttpTransportSE;
导入android.app.Activity;
导入android.graphics.Bitmap;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类FirstScreen扩展活动
{
/**在首次创建活动时调用*/
私有静态字符串SOAP_ACTION1=”http://tempuri.org/FahrenheitToCelsius";
私有静态字符串SOAP_ACTION2=”http://tempuri.org/CelsiusToFahrenheit";
私有静态字符串命名空间=”http://tempuri.org/";
私有静态字符串方法\u NAME1=“华氏摄氏度”;
私有静态字符串方法\u NAME2=“CelsiusToFahrenheit”;
专用静态字符串URL=”http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
按钮btnFar、btnCel、btnClear;
编辑文本txtFar,txtCel;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u第一屏幕);
btnFar=(按钮)findViewById(R.id.btnFar);
btnCel=(按钮)findViewById(R.id.btnCel);
btnClear=(按钮)findviewbyd(R.id.btnClear);
txtFar=(EditText)findViewById(R.id.txtFar);
txtCel=(EditText)findViewById(R.id.txtCel);
btnFar.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
新的one1().execute();
}
});
btnCel.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
新的one2().execute();
}
});
setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
txtCel.setText(“”);
txtFar.setText(“”);
}
});
}
私有类one1扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间,方法\名称1);
//使用此选项可以添加参数
request.addProperty(“华氏度”,txtFar.getText().toString());
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
调用(SOAP_ACTION1,信封);
//从信封正文获取SoapResult。
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(结果!=null)
{
//获取第一个属性并更改标签文本
setText(result.getProperty(0.toString());
}
其他的
{
Toast.makeText(getApplicationContext(),“无响应”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}
私有类one2扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间,方法\名称2);
//使用此选项可以添加参数
request.addProperty(“摄氏度”,txtCel.getText().toString());
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
调用(SOAP_ACTION2,信封);
//从信封正文获取SoapResult。
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(结果!=null)
{
//获取第一个属性并更改标签文本
setText(result.getProperty(0.toString());
}
其他的
{
Toast.makeText(getApplicationContext(),“无响应”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e、 printStackTrace();
}返回null;
}
}
}
错误日志和代码
private class one1 extends AsyncTask<String, Integer, SoapObject> {
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();
}
}
}
}
私有类one1扩展了异步任务{
受保护的SoapObject doInBackground(字符串…arg0){
受保护**void**onPreExecute(**)**
{
//textview/edittext中的值
}
//TODO自动生成的方法存根
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间,方法\名称1);
//使用此选项可以添加参数
request.addProperty(“华氏”,“30”);//在此处提供值
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
信封
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<String, Integer, SoapObject> {
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();
}
}
}
}
Void onPostExecute(int result)
{
if(result != null)
{
txtFar.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
}
new SoapRequestTask().execute();//call asyntask from ui.
private class SoapRequestTask extends AsyncTask<Void, Void, Long> {
protected void onPreExecute() {
//display progress dialog
}
protected Long doInBackground(Void... params) {
//make soap request and get result. do not update ui here.
return result;
}
protected void onPostExecute(Long result) {
//dismiss dialog. update ui here.
}
}
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_main);
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<String, Integer, SoapObject> {
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();
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="FahrenheitToCelsius" />
<EditText
android:id="@+id/ed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="43dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="37dp"
android:text="MakeRequest" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Result" />
</LinearLayout>
txtFar.setText(result.getProperty(0).toString());
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();