Ich versuche, den folgenden Code zu verwenden, um eine Webmethod zu erstellen und sie dann von einem Android-App mit OkHttp:Versuch Webmethod von Android-App für den Zugriff mit OkHttp3
webservice unter http: // Someaddress .com/Hello World
using GLS.GLIS.Core;
using System.IO;
using GLS.GLIS.ApplicationServices.Settings;
namespace GLS.GLIS.Web.Ax
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Wireless : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
Above Werke, Produktionscode
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whirlpool.ad.na.gruves1.glisweb">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
MainActivity.java
package com.whirlpool.ad.na.gruves1.glisweb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button HelloWorldbtn = (Button) findViewById(R.id.btn1);
HelloWorldbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onHelloWorld(v);
}
});
}
public void onHelloWorld (View v) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://<someaddress>.com/HelloWorld")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
Das Ziel ist die Android-App zu verwenden, um den Webservice Hello World und onResponse, Toast "Erfolg" zu treffen. Mein Ziel ist es, in zukünftigen Iterationen darauf aufzubauen und mehr Webservices zu nutzen. Im Folgenden werden jedoch verschiedene Fehler zurückgegeben, die bei der Suche keine Lösungen zu meinem Problem zurückgeben. Das Beste, was ich mir im Moment vorstellen kann, ist, dass es vielleicht ein Aktivitätsproblem ist. Das Brechen des Codes und das Folgen des Stack-Trace gibt nichts zurück, was mir auch noch auffällt. Jede Hilfe würde sehr geschätzt werden. Danke im Voraus.
aktualisieren
Im Folgenden ist der Fehler, den ich bekommen, wenn sie in einem Emulator ausgeführt wird:
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: glisweb, PID: 2749
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:345)
at android.widget.Toast.<init>(Toast.java:101)
at android.widget.Toast.makeText(Toast.java:259)
at glisweb.MainActivity$2.onResponse(MainActivity.java:54)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)