如何解决Android中的此错误?java.net.MalformedURLException:找不到协议:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21507691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve this error in Android? java.net.MalformedURLException: Protocol not found:
提问by Ganesh Katikar
I dont know what is exactly problem, But I am getting error like: Even I also checking all paths, and it is correct but still it will get error.
我不知道究竟是什么问题,但我收到错误,例如:即使我也检查所有路径,它是正确的,但仍然会出错。
java.net.MalformedURLException: Protocol not found: www.w3schools.com/webservices/tempconvert.asmx
java.net.MalformedURLException:找不到协议:www.w3schools.com/webservices/tempconvert.asmx
My Code:
我的代码:
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;
}
}
I dont know what is exact problem but I am getting error like:
我不知道什么是确切的问题,但我收到如下错误:
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)
采纳答案by Oleksii K.
Add http://
before the www...
or https://
.
http://
在www...
或之前添加https://
。
public String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
回答by CONvid19
you missed the protocol http://
你错过了协议 http://
public String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
NOTE:
If you just need to convert from Celsius to Fahrenhiet ot vice-versa you could try this:
注意:
如果您只需要从摄氏度转换为华氏度,反之亦然,您可以尝试以下操作:
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 ); }