Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ';未处理的异常';在android studio中尝试设置HttpURLConnection时_Java_Android_Web Services_Android Studio - Fatal编程技术网

Java ';未处理的异常';在android studio中尝试设置HttpURLConnection时

Java ';未处理的异常';在android studio中尝试设置HttpURLConnection时,java,android,web-services,android-studio,Java,Android,Web Services,Android Studio,我尝试设置HttpURLConnection。我使用的语法来自。我想将字符串“phonenumber”和字符串“password”发送到Web服务器。这是我的.java文件: public class Login extends AppCompatActivity { @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceS

我尝试设置HttpURLConnection。我使用的语法来自。我想将字符串“phonenumber”和字符串“password”发送到Web服务器。这是我的.java文件:

public class Login extends AppCompatActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        EditText phonenumberText = (EditText)findViewById(R.id.phonenumberText);
        EditText passwordText = (EditText)findViewById(R.id.passwordText);
        String phonenumber = phonenumberText.getText().toString();
        String password = passwordText.getText().toString();
        String web = "webadress/login/tel=" + phonenumber + "&password =" + password;

        URL url = new URL(web);
        HttpURLConnection client = (HttpURLConnection) url.openConnection();

        try{
            InputStream in = new BufferedInputStream(client.getInputStream());
            readStream(in);
            finally {
                client.disconnect();
            }//finally
        }//try

    }//onCreate
}//Login
在我包含的AndroidManifest中

<uses-permission android:name="android.permission.INTERNET" />

但是对于方法
url.openConnection()
client.getInputStream()
readStream(in)
我得到了错误未处理的异常:java.io.IOException。对于
新URL(web)
我得到了错误未处理的异常:java.net.MalformedURLException。非常感谢您的帮助。

您需要将引发
IoException
的所有语句包装在try/catch块中,而不仅仅是最后两个。大概是这样的:

//your existing code..

HttpURLConnection client = null;

try{
    URL url = new URL(web);
    client = (HttpURLConnection) url.openConnection();

    InputStream in = new BufferedInputStream(client.getInputStream());
    readStream(in);

} catch (MalformedURLException e) {
     //bad  URL, tell the user
} catch (IOException e) {
     //network error/ tell the user
} finally  {
     client.disconnect();
}

具体地说:
catch
块用于“捕获”异常,例如,当出现网络错误或URL无效时,控制权会转移到
catch
块,从那里您可以告诉用户有错误。

阅读以下内容:您正在调用抛出已检查异常的方法,因此你需要抓住它们。这就是它的工作原理。我是一个初学者:捕捉方法意味着什么?你不是在捕捉方法,你需要捕捉方法抛出的异常。谢谢,但它无法解析客户端。仅在'client.disconnect()'中@Izotz,已修复,抱歉。您需要放置
HttpURLConnection客户端
在catch块之外finally语句“可能尚未初始化”中的变量“client”@Izotz,Oops再次修复,抱歉,应该是
HttpURLConnection client=null