"java.net.MalformedURLException: Protocol not found" 读取 HTML 文件。

11

我收到了一个错误:java.net.MalformedURLException: Protocol not found

我想读取网络上的一个HTML文件。

mainfest :::::   uses-permission android:name="android.permission.INTERNET"

uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 

import com.doviz.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {

public String inputLine;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String myUri = "";
    myUri = "www.tcmb.gov.tr/kurlar/today.html";


    Toast.makeText( this, "step-1 " , Toast.LENGTH_LONG).show();
    try{
            Toast.makeText( this, "step -2" , Toast.LENGTH_LONG).show();
            myUri = "www.tcmb.gov.tr/kurlar/today.html";

        URL url = new URL(myUri);

        Toast.makeText( this, "step-3" , Toast.LENGTH_LONG).show();
            final InputStream is =url.openStream();
            Toast.makeText( this, "step -4" , Toast.LENGTH_LONG).show();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            Toast.makeText( this, "step -5 " , Toast.LENGTH_LONG).show();   
        String line;
        Toast.makeText( this, "step-6" , Toast.LENGTH_LONG).show();
        while ((line=reader.readLine())!=null){
           // page.add(line);
        }
        Toast.makeText( this, " step-7" , Toast.LENGTH_LONG).show();
    }
    catch(Exception e){
        //e.printStackTrace();
        TextView tx =(TextView)findViewById(id.TextView1); 
        tx.setText(myUri + " >>> "+  e.getMessage());
        Toast.makeText( this, "problem = " + e.getMessage() + " -- "+ e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        //System.exit(1);
    }

    Toast.makeText( this, "step -8" , Toast.LENGTH_LONG).show();



}
2个回答

38

你的URI不是一个URI。它缺少协议组件。它需要http://或其他你想要使用的协议。


0
String myUri = "";



myUri = "www.tcmb.gov.tr/kurlar/today.html";

你的URI还没有完整,请编写完整的URL,就像这样。

String myUri="https://www.tcmb.gov.tr/kurlar/today.html";

1
如果你打算在使用之前或者紧接着的下一行赋值,那么就不应该初始化它。 - user207421

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接