简体   繁体   English

如何从 Android Studio 项目调用 web 服务

[英]How to call a web service from Android Studio project

I am new to Android and Java so excuse my lack of knowledge.我是 Android 和 Java 的新手,所以请原谅我缺乏知识。 Taking this time during quarantine and trying to get a response to a web service I wrote in .NET It is a simple service.在隔离期间利用这段时间并尝试获得对我在 .NET 中编写的 web 服务的响应 这是一个简单的服务。 Pass userid and passwd and it returns the user last name if found or NO if not found.传递 userid 和 passwd,如果找到则返回用户姓氏,如果找不到则返回 NO。 I have a VB 2017 program that is working as expected but trying to also have the same results in an Android environment.我有一个 VB 2017 程序,它按预期工作,但试图在 Android 环境中也有相同的结果。

Here are my imports.这是我的进口商品。

import java.io.IOException;
import java.net.SocketException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

On a button click I have the following.单击按钮,我有以下内容。

{
  String userID = "Tom";
  String pwd = "&yhe$43*";

  userid = new PropertyInfo();
  password = new PropertyInfo();

  SoapObject request = new SoapObject("http://tempuri.org/", "WebService1");

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

  userid.setName("userID");
  userid.setValue(userID);
  userid.setType(String.class);
  password.setName("password");
  password.setValue("Yo");
  password.setType(String.class);

  request.addProperty(userid);
  request.addProperty(password);
  try {
    httpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
    String resp = response.toString();
    Toast.makeText(MainActivity.this, (CharSequence) resp, Toast.LENGTH_LONG).show();
  } catch (Exception e) {
    Toast.makeText(MainActivity.this, (CharSequence) e, Toast.LENGTH_LONG).show();
  }
}

Error is happening on this line:此行发生错误:

httpTransport.call(SOAP_ACTION, envelope); 

Looks like it is getting a看起来它正在得到一个

java.lang.reflect.InvocationTargetException

Is there something obviously wrong here.这里有什么明显的错误吗? I could use some guidance.我可以使用一些指导。

Here is data Logcat:这是数据 Logcat:

java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
    at android.view.View.performClick(View.java:6294) 
    at android.view.View$PerformClick.run(View.java:24770) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: android.os.NetworkOnMainThreadException cannot be cast to java.lang.CharSequence
    at com.example.myapplication.MainActivity.getName(MainActivity.java:71)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
    at android.view.View.performClick(View.java:6294) 
    at android.view.View$PerformClick.run(View.java:24770) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

The problem is this line, I would assume:问题是这一行,我假设:

       Toast.makeText(MainActivity.this, (CharSequence) e, Toast.LENGTH_LONG).show();

You're casting e (of type "Exception") to a CharSequence, which is obviously illegal, since a class can't inherit from both Exception and CharSequence.您将 e(类型为“Exception”)转换为 CharSequence,这显然是非法的,因为 class 不能同时继承 Exception 和 CharSequence。

Try using "e.getMessage()" instead of "(CharSequence) e".尝试使用“e.getMessage()”而不是“(CharSequence) e”。 That won't fix the actual cause of the exception but at least you will be able to see what the actual exception is.这不会解决异常的实际原因,但至少您将能够看到实际的异常是什么。

android.os.NetworkOnMainThreadException 

From the error log you posted, I can see that you are doing a Network task in the android main thread.从您发布的错误日志中,我可以看到您正在 android 主线程中执行网络任务。

this exception comes when you do Network operation on the main thread.当您在主线程上执行网络操作时会出现此异常。 please schedule this task in a background thread.请在后台线程中安排此任务。

For your reference, I have included a link that describes how to solve this How to fix 'android.os.NetworkOnMainThreadException'?供您参考,我提供了一个链接,该链接描述了如何解决此问题如何修复“android.os.NetworkOnMainThreadException”?

And also change this也改变这个

Toast.makeText(MainActivity.this, (CharSequence) e, Toast.LENGTH_LONG).show();

to

Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();

finally, make sure you have added Inte.net permission in the manifest file.最后,确保您在清单文件中添加了 Inte.net 权限。

Happy coding!!编码快乐!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM