[英]Android HttpURLConnection AndroidRuntime error
Problems are solved. 问题解决了。 The cause is that I forgot to give Internet permission to the application.
原因是我忘记授予该应用程序Internet权限。
The basic function I want to implement is that 我要实现的基本功能是
I use a asynctask to load results from remote server. 我使用asynctask从远程服务器加载结果。 However, I have been debugging the following codes for hours, and still get no clue where goes wrong.
但是,我已经调试了以下代码几个小时,但仍然不知道哪里出了问题。 Can anyone help me out?
谁能帮我吗?
private class ReportLocationTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return reportLoc(urls[0]);
} catch (IOException e) {
return getResources().getString(R.string.connection_error);
}
}
@Override
protected void onPostExecute(String result) {
mActivityIndicator.setVisibility(View.GONE);
mReport.setText(result);
}
}
private String reportLoc(String urlstring) throws IOException{
URL url = new URL(urlstring);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try{
InputStream in = new BufferedInputStream(conn.getInputStream());
InputStreamReader is = new InputStreamReader(in);
StringBuilder sb=new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
//System.out.println(read);
sb.append(read);
read =br.readLine();
}
return sb.toString();
}
finally {
conn.disconnect();
}
}
you haven't specified a request method. 您尚未指定请求方法。 i assume you want to GET the results from the server.
我假设您想从服务器获取结果。
HttpURLConnection conn = (HttpURLConnection ) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(10000); // millis
conn.setConnectTimeout(15000); // millis
conn.setDoOutput(true);
conn.connect();
int response = conn.getResponseCode();
if (response == HttpStatus.SC_OK) {
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.