gpt4 book ai didi

android - 从 android 调用 web 服务的示例应用程序返回错误 "Unfortunately,MyTest has stopped"

转载 作者:搜寻专家 更新时间:2023-11-01 08:57:17 25 4
gpt4 key购买 nike

我有一个代码,可以调用 www,tempuri.org 提供的网络服务。当我尝试运行该应用程序时,该应用程序意外停止并显示消息 - “不幸的是,MyTest 已停止”。这是我的 java 文件 -

主要 Activity .java :-

package com.example.mytest;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
public static final String SOAP_ACTION = "http://tempuri.org/CelciusToFarenheit";
public static final String METHOD_NAME = "CelciusToFarenheit";
public static final String NAMESPACE= "http://tempuri.org/";
public static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
SoapEnvelope se;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.TextView01);

SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);
Request.addProperty("Celcius","32");
SoapSerializationEnvelope sse = new SoapSerializationEnvelope (se.VER11);
sse.dotNet=true;
sse.setOutputSoapObject(Request);
HttpTransportSE abt = new HttpTransportSE(URL);
try
{
abt.call(SOAP_ACTION,se);
SoapObject resultString=(SoapObject)sse.getResponse();
String resultData=resultString.getProperty(0).toString();

tv.setText("Status :"+resultString);
}
catch(Exception e)
{
e.printStackTrace();
}





}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

activity_main.xml :-

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

logcat如下——

08-10 01:35:10.421: E/AndroidRuntime(1077): FATAL EXCEPTION: main
08-10 01:35:10.421: E/AndroidRuntime(1077): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
08-10 01:35:10.421: E/AndroidRuntime(1077): at com.example.mytest.MainActivity.onCreate(MainActivity.java:30)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.Activity.performCreate(Activity.java:5133)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.os.Looper.loop(Looper.java:137)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-10 01:35:10.421: E/AndroidRuntime(1077): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 01:35:10.421: E/AndroidRuntime(1077): at java.lang.reflect.Method.invoke(Method.java:525)
08-10 01:35:10.421: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-10 01:35:10.421: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-10 01:35:10.421: E/AndroidRuntime(1077): at dalvik.system.NativeStart.main(Native Method)

我刚开始使用 android 开发 Web 服务。你能建议我如何处理吗?

最佳答案

像这样使用:

XML:

      <RelativeLayout 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" >

<Button
android:id="@+id/web_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="112dp"
android:layout_marginTop="146dp"
android:text="Web Service" />

<TextView
android:id="@+id/fetch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />

</RelativeLayout>

Activity :

  public class MainActivity extends Activity {

Button web_service;
TextView fetch_service;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fetch_service = (TextView) findViewById(R.id.fetch);
web_service = (Button) findViewById(R.id.web_service);
web_service.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myasynctask MAT = new myasynctask();
MAT.execute();
}
});
}

class myasynctask extends AsyncTask<String, Void, String> {

String str;
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

@Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE ht = new HttpTransportSE(URL);

try {
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
str = response.toString();

} catch (Exception e) {
e.printStackTrace();

}
Log.d("WebRespone", str);
return str;
}

@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
fetch_service.setText(result);
super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

}
}

list :在您的 list 中添加以下内容

 <uses-permission android:name="android.permission.INTERNET"/>

输出: enter image description here

希望对您有所帮助。

关于android - 从 android 调用 web 服务的示例应用程序返回错误 "Unfortunately,MyTest has stopped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com