简体   繁体   English

java.net.URL 在 android .. 新手问题

[英]java.net.URL in android .. newbie question

I am a newbie in java and was trying android development.我是 java 的新手,正在尝试 android 开发。 The following code generated malformedURLException.can someone help me identify the exception.以下代码生成了 malformedURLException。有人可以帮我识别异常。 Any hint would be highly helpful任何提示都会非常有帮助

package com.example.helloandroid;

import android.app.Activity;
//import android.widget.TextView;
import android.os.Bundle;
import java.net.*;
import java.io.*;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        String outdata = "";
        URL url_g = new URL("http://www.google.com/");
        URLConnection ukr = url_g.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(ukr.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            outdata += inputLine;
        in.close();
       tv.setText(outdata);
       setContentView(tv);
    }
}

This is because URL constructor ( new URL("http://www.google.com/") ) throws a Checked Exception, in this case MalformedURLException , which means that you have to catch it or declare it in your method.这是因为 URL 构造函数( new URL("http://www.google.com/") )抛出了一个检查异常,在这种情况下是MalformedURLException ,这意味着你必须在你的方法中捕获或声明它。

Use a try/catch or the throws clause in the onCreate method.在 onCreate 方法中使用 try/catch 或 throws 子句。

try/catch solution:尝试/捕获解决方案:

public void onCreate(Bundle savedInstanceState) {
    ...
    try {
        URL url_g = new URL("http://www.google.com/");
    } catch(MalformedURLException e) {
        //Do something with the exception.
    }
    ...
}

Throws solution:抛出解决方案:

@Override
public void onCreate(Bundle savedInstanceState) throws MalformedURLException {
    ...
    URL url_g = new URL("http://www.google.com/");
    ...
}

Check this for more information on exceptions.检查此以获取有关异常的更多信息。

http://download.oracle.com/javase/tutorial/essential/exceptions/ http://download.oracle.com/javase/tutorial/essential/exceptions/

Well you need to execute your code in a try/catch method for first.那么你首先需要在 try/catch 方法中执行你的代码。 Try it and let c what you have more?试试看,让 c 你还有什么?

One thing to note about OP code is that even after you add the required exception handling, the code won't run because Android doesn't allow network activities in the main thread.关于 OP 代码需要注意的一点是,即使添加了所需的异常处理,代码也不会运行,因为 Android 不允许主线程中的网络活动。 You need to use an AsyncTask, as described here: http://developer.android.com/resources/articles/painless-threading.html您需要使用 AsyncTask,如下所述: http://developer.android.com/resources/articles/painless-threading.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM