こんにちは、Android を使用して Web サービスからデータを読み込もうとしましたが、試行するたびに問題に直面します
今日、私は 3schools サイトから Web サービスを読み取ろうとしています
このコードを使用する
package com.example.lo2i03;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
//import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private static String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.txt);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius","32");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
tv.setText(result.getProperty(0).toString());
}
catch (Exception e)
{
tv.setText("Error");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
インターネットにアクセスするためのマニフェストに許可を追加したことを知って
それはキャッチブロックに直接行きます...
マニフェスト ファイル
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lo2i03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.lo2i03.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>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
Android バージョン: 4.2 - 4.2
LogCat エラーは次のようになります
11-24 01:28:29.065: E/Trace(13408): error opening trace file: No such file or directory (2)
11-24 01:28:29.195: D/AndroidRuntime(13408): Shutting down VM
11-24 01:28:29.195: W/dalvikvm(13408): threadid=1: thread exiting with uncaught exception (group=0x412862a0)
11-24 01:28:29.195: E/AndroidRuntime(13408): FATAL EXCEPTION: main
11-24 01:28:29.195: E/AndroidRuntime(13408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lo2i03/com.example.lo2i03.MainActivity}: android.os.NetworkOnMainThreadException
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.os.Looper.loop(Looper.java:137)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-24 01:28:29.195: E/AndroidRuntime(13408): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 01:28:29.195: E/AndroidRuntime(13408): at java.lang.reflect.Method.invoke(Method.java:511)
11-24 01:28:29.195: E/AndroidRuntime(13408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-24 01:28:29.195: E/AndroidRuntime(13408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-24 01:28:29.195: E/AndroidRuntime(13408): at dalvik.system.NativeStart.main(Native Method)
11-24 01:28:29.195: E/AndroidRuntime(13408): Caused by: android.os.NetworkOnMainThreadException
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-24 01:28:29.195: E/AndroidRuntime(13408): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-24 01:28:29.195: E/AndroidRuntime(13408): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-24 01:28:29.195: E/AndroidRuntime(13408): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-24 01:28:29.195: E/AndroidRuntime(13408): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-24 01:28:29.195: E/AndroidRuntime(13408): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
11-24 01:28:29.195: E/AndroidRuntime(13408): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:146)
11-24 01:28:29.195: E/AndroidRuntime(13408): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
11-24 01:28:29.195: E/AndroidRuntime(13408): at com.example.lo2i03.MainActivity.onCreate(MainActivity.java:35)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.Activity.performCreate(Activity.java:5191)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-24 01:28:29.195: E/AndroidRuntime(13408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-24 01:28:29.195: E/AndroidRuntime(13408): ... 11 more
新しいログキャット (1.0)
11-24 16:28:28.850: E/Trace(25195): error opening trace file: No such file or directory (2)
11-24 16:28:28.995: D/libEGL(25195): loaded /system/lib/egl/libEGL_mali.so
11-24 16:28:28.995: D/libEGL(25195): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-24 16:28:29.000: D/libEGL(25195): loaded /system/lib/egl/libGLESv2_mali.so
11-24 16:28:29.000: D/(25195): Device driver API match
11-24 16:28:29.000: D/(25195): Device driver API version: 10
11-24 16:28:29.000: D/(25195): User space API version: 10
11-24 16:28:29.000: D/(25195): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Wed Sep 12 17:53:53 KST 2012
11-24 16:28:29.030: D/OpenGLRenderer(25195): Enabling debug mode 0
11-24 16:28:31.215: D/dalvikvm(25195): GC_CONCURRENT freed 214K, 10% free 12330K/13575K, paused 13ms+13ms, total 40ms
11-24 16:28:39.615: W/IInputConnectionWrapper(25195): getSelectedText on inactive InputConnection
11-24 16:28:39.615: W/IInputConnectionWrapper(25195): setComposingText on inactive InputConnection
11-24 16:28:39.615: W/IInputConnectionWrapper(25195): getExtractedText on inactive InputConnection
インターネットにアクセスするために (WiFi) を使用することを知っている。
何かアドバイス?