【问题标题】:"java.net.MalformedURLException: Protocol not found" read to html file“java.net.MalformedURLException:找不到协议”读取到 html 文件
【发布时间】:2012-10-17 12:45:05
【问题描述】:

我收到一个错误: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();



}

【问题讨论】:

    标签: java


    【解决方案1】:

    您的 URI 不是 URI。没有协议组件。它需要 http:// 或您想要的任何其他协议。

    【讨论】:

      【解决方案2】:
      String myUri = "";
      
      
      
      myUri = "www.tcmb.gov.tr/kurlar/today.html";
      

      您的 uri 未完成。您可以编写完整的 url 就像一样。

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

      【讨论】:

      • ...如果你要在使用前分配它,你不应该初始化它,尤其是在下一行。
      猜你喜欢
      • 2018-11-08
      • 2012-09-03
      • 2023-04-03
      • 2010-12-14
      • 2014-05-26
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      相关资源
      最近更新 更多