2017-01-17 74 views
0

enter image description here鏈接在瀏覽器,但不是在HttpURLConnection的工作

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()我變空。 當我在瀏覽器中打開鏈接它呼應的結果,但沒有迴音結果的Android HttpURLConnection的

  1. 列表項

回答

0

錯誤說你沒有權限訪問您請求的資源。使用HttpUrlConnection撥打電話時,您需要傳遞登錄名/密碼。

它在瀏覽器中工作的原因很可能是您已經在瀏覽器中登錄。

+0

我沒有登錄到這個瀏覽器,但是這個鏈接在瀏覽器合作,以呼應的結果,但在Adnroid內 – Src7

+0

不工作,你能告訴我什麼是響應代碼。你可以得到它如下:httpurlconnection.getResponseCode() –

+0

錯誤403禁止 – Src7

相關問題