我尝试设置HttpURLConnection。我使用Google documentation的语法。我想将字符串'phonenumber'和字符串'password'发送到Web服务器。这是我的java文件:尝试在android studio中设置HttpURLConnection时出现'未处理的异常'
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的。对于new URL(web)
,我得到错误未处理的异常:java.net.MalformedURLException。帮助将不胜感激。
阅读此:https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html –
您正在调用抛出检查异常的方法,因此您需要捕获它们。这就是它的工作原理。 – Mena
我是一名初学者:捕捉方法的含义是什么? – Izotz