正確には何が問題なのかわかりませんが、次のようなエラーが発生します。
Java.net.MalformedURLException:プロトコルが見つかりません:www.w3schools.com/webservices/tempconvert.asmx
私のコード:
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.AndroidHttpTransport;
import Android.app.Activity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.widget.TextView;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
public final String NAMESPACE = "http://tempuri.org";
public String URL = "www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
public static String TAG = "MyTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "MainActivity Started");
tv = (TextView) findViewById(R.id.txt1);
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Celsius", "32");
// URL=URL.replaceAll(" ", "%20");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
Log.d(TAG, "URL:"+URL);
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try {
// HttpTransportSE aht = new HttpTransportSE(URL);
Log.d(TAG, "aht:" + aht);
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
.getResponse();
// Object resultString = (Object)soapEnvelope.getResponse();
Log.d(TAG, "result String=" + resultString);
tv.setText("Status: " + resultString);
} catch (Exception e) {
Log.d(TAG, "Error: ", e);
}
}
@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;
}
}
正確な問題が何であるかわかりませんが、次のようなエラーが発生します。
02-01 21:07:01.264: D/MyTest(1068): MainActivity Started
02-01 21:07:01.534: D/MyTest(1068): Error:
02-01 21:07:01.534: D/MyTest(1068): Java.net.MalformedURLException: Protocol not found: www.w3schools.com/webservices/tempconvert.asmx
02-01 21:07:01.534: D/MyTest(1068): at Java.net.URL.<init>(URL.Java:275)
02-01 21:07:01.534: D/MyTest(1068): at Java.net.URL.<init>(URL.Java:159)
02-01 21:07:01.534: D/MyTest(1068): at org.ksoap2.transport.ServiceConnectionSE.<init>(ServiceConnectionSE.Java:65)
02-01 21:07:01.534: D/MyTest(1068): at org.ksoap2.transport.ServiceConnectionSE.<init>(ServiceConnectionSE.Java:61)
02-01 21:07:01.534: D/MyTest(1068): at org.ksoap2.transport.AndroidServiceConnection.<init>(AndroidServiceConnection.Java:27)
02-01 21:07:01.534: D/MyTest(1068): at org.ksoap2.transport.AndroidHttpTransport.getServiceConnection(AndroidHttpTransport.Java:35)
02-01 21:07:01.534: D/MyTest(1068): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.Java:124)
02-01 21:07:01.534: D/MyTest(1068): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.Java:95)
追加 http://
の前に www...
またはhttps://
。
public String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
プロトコルを見逃しましたhttp://
public String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
注:
摂氏から華氏またはその逆に変換する必要があるだけの場合、これを試すことができます:
function C2F ( C ) {
var F = eval ((C x 9 / 5) +32)
return ( F ); }
function F2C ( F ) {
var C = eval ( F - 32 ) * 5 / 9;
return ( C ); }