您好我一直试图让Web服务登录工作一段时间。在我输入用户名和密码并按下登录后,应用程序屏幕刚刚关闭,“很遗憾,RemoteLogin已停止工作。”我已经尝试通过日志猫查看,但我自己也无法理解它,我以前从未在Android中使用过Web服务并且在努力。这是我的主要活动:
package com.example.remotelogin;
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.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText txtName, txtPassword;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
txtName = (EditText) findViewById(R.id.edit_Name);
txtPassword = (EditText) findViewById(R.id.edit_Pwd);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String userName = txtName.getText().toString();
String userPwd = txtPassword.getText().toString();
if (userName.trim().equals(""))
txtName.setError("Please enter username");
if (userPwd.trim().equals(""))
txtPassword.setError("Please enter password");
if (!userName.trim().equals("") && !userPwd.trim().equals(""))
{
new GetAuthentication(MainActivity.this).execute("RemoteLogin",userName, userPwd);
}
}
});
}
private class GetAuthentication extends AsyncTask<String, Void, String>
{
private static final String NAMESPACE = "http://tempuri.org/";
//Webservice URL - WSDL File location
private static final String URL = "http://84.51.246.251/ttms/remoteaccess.asmx?WSDL";
ProgressDialog pd ;
private Context _context;
public GetAuthentication(Context context) {
_context = context;
}
protected void onPreExecute ()
{
super.onPreExecute();
pd = new ProgressDialog(_context);
pd.setTitle("Processing");
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
if (params[0].trim().equals(""))
return "";
SoapObject request = new SoapObject(NAMESPACE, params[0]);
request.addProperty("userName", params[1]);
request.addProperty("password", params[2]);
// params[0] --> function name that you have passed from activity
// params[1] --> username name that you have passed from activity
// params[2] --> password that you have passed from activity
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
try
{
httpTransport.call(NAMESPACE + params[0], envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return response.toString();
}
catch (Exception e)
{
if (pd.isShowing())
pd.dismiss();
return "Error";
}
}
protected void onPostExecute(String str)
{
super.onPostExecute(str);
try
{
if (pd.isShowing())
pd.dismiss();
}
catch (Exception e){}
if (str.equalsIgnoreCase("Error"))
Toast.makeText(getApplicationContext(), "Please try again after some time.", Toast.LENGTH_SHORT).show();
else
{
// here parse your json or whatever you are retrieving from the server
}
}
}
}
activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:hint="Username"
android:padding="10dp"
android:inputType="text"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/edit_Pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi"
android:hint="Password"
android:padding="10dp"
android:inputType="textPassword" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.remotelogin"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的:
07-23 10:22:20.347: D/libEGL(8567): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-23 10:22:20.347: D/libEGL(8567): loaded /system/lib/egl/libGLESv2_mali.so
07-23 10:22:20.397: D/OpenGLRenderer(8567): Enabling debug mode 0
07-23 10:22:56.842: E/dalvikvm(8567): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.remotelogin.MainActivity$GetAuthentication.doInBackground
07-23 10:22:56.842: W/dalvikvm(8567): VFY: unable to resolve new-instance 1123 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/example/remotelogin/MainActivity$GetAuthentication;
07-23 10:22:56.842: D/dalvikvm(8567): VFY: replacing opcode 0x22 at 0x0013
07-23 10:22:56.842: D/dalvikvm(8567): DexOpt: unable to opt direct call 0x2209 at 0x19 in Lcom/example/remotelogin/MainActivity$GetAuthentication;.doInBackground
07-23 10:22:56.842: D/dalvikvm(8567): DexOpt: unable to opt direct call 0x220c at 0x2f in Lcom/example/remotelogin/MainActivity$GetAuthentication;.doInBackground
07-23 10:22:56.842: I/dalvikvm(8567): DexOpt: unable to optimize instance field ref 0x0ddd at 0x32 in Lcom/example/remotelogin/MainActivity$GetAuthentication;.doInBackground
07-23 10:22:56.842: D/dalvikvm(8567): DexOpt: unable to opt direct call 0x220f at 0x3b in Lcom/example/remotelogin/MainActivity$GetAuthentication;.doInBackground
07-23 10:22:56.882: W/dalvikvm(8567): threadid=12: thread exiting with uncaught exception (group=0x40bc0318)
07-23 10:22:56.892: E/AndroidRuntime(8567): FATAL EXCEPTION: AsyncTask #1
07-23 10:22:56.892: E/AndroidRuntime(8567): java.lang.RuntimeException: An error occured while executing doInBackground()
07-23 10:22:56.892: E/AndroidRuntime(8567): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-23 10:22:56.892: E/AndroidRuntime(8567): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.lang.Thread.run(Thread.java:856)
07-23 10:22:56.892: E/AndroidRuntime(8567): Caused by: java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
07-23 10:22:56.892: E/AndroidRuntime(8567): at com.example.remotelogin.MainActivity$GetAuthentication.doInBackground(MainActivity.java:108)
07-23 10:22:56.892: E/AndroidRuntime(8567): at com.example.remotelogin.MainActivity$GetAuthentication.doInBackground(MainActivity.java:1)
07-23 10:22:56.892: E/AndroidRuntime(8567): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 10:22:56.892: E/AndroidRuntime(8567): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-23 10:22:56.892: E/AndroidRuntime(8567): ... 5 more
07-23 10:22:57.263: E/WindowManager(8567): Activity com.example.remotelogin.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4141c938 that was originally added here
07-23 10:22:57.263: E/WindowManager(8567): android.view.WindowLeaked: Activity com.example.remotelogin.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4141c938 that was originally added here
07-23 10:22:57.263: E/WindowManager(8567): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
07-23 10:22:57.263: E/WindowManager(8567): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
07-23 10:22:57.263: E/WindowManager(8567): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-23 10:22:57.263: E/WindowManager(8567): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-23 10:22:57.263: E/WindowManager(8567): at android.view.Window$LocalWindowManager.addView(Window.java:547)
07-23 10:22:57.263: E/WindowManager(8567): at android.app.Dialog.show(Dialog.java:285)
07-23 10:22:57.263: E/WindowManager(8567): at com.example.remotelogin.MainActivity$GetAuthentication.onPreExecute(MainActivity.java:95)
07-23 10:22:57.263: E/WindowManager(8567): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-23 10:22:57.263: E/WindowManager(8567): at android.os.AsyncTask.execute(AsyncTask.java:534)
07-23 10:22:57.263: E/WindowManager(8567): at com.example.remotelogin.MainActivity$1.onClick(MainActivity.java:61)
07-23 10:22:57.263: E/WindowManager(8567): at android.view.View.performClick(View.java:4103)
07-23 10:22:57.263: E/WindowManager(8567): at android.view.View$PerformClick.run(View.java:17117)
07-23 10:22:57.263: E/WindowManager(8567): at android.os.Handler.handleCallback(Handler.java:615)
07-23 10:22:57.263: E/WindowManager(8567): at android.os.Handler.dispatchMessage(Handler.java:92)
07-23 10:22:57.263: E/WindowManager(8567): at android.os.Looper.loop(Looper.java:137)
07-23 10:22:57.263: E/WindowManager(8567): at android.app.ActivityThread.main(ActivityThread.java:4744)
07-23 10:22:57.263: E/WindowManager(8567): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 10:22:57.263: E/WindowManager(8567): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 10:22:57.263: E/WindowManager(8567): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-23 10:22:57.263: E/WindowManager(8567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-23 10:22:57.263: E/WindowManager(8567): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
将肥皂罐包含在库中以及构建路径中。