public class DPLoader extends AsyncTask<String, Void, String> {
Context context;
DPLoader(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... params) {
String login_url = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
try {
URL url = new URL(login_url);
HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoInput(true);
//下面我尝试阅读但没有得到结果
InputStream inputstream = httpurlconnection.getInputStream();
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedreader.readLine()) != null) {
result += line;
}
bufferedreader.close();
inputstream.close();
httpurlconnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "null";
}
@Override
protected void onPreExecute() {
// Toast.makeText(context, ""+ usernameEt.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(context, ""+result, Toast.LENGTH_SHORT).show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
图像是我尝试使用httpurlconnection.getErrorstream()`` and with
httpurlconnection.getInputStream()的结果toast,我得到了null。
当我在浏览器中打开链接时,它会回显结果,但不会在android httpurlconnection
答案 0 :(得分:0)
错误表示您无权访问您请求的资源。使用HttpUrlConnection进行呼叫时需要传递登录名/密码。
它在浏览器中工作的原因很可能是您已经在浏览器中登录。