我嘗試設置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