2017-03-17 71 views
0

這是我的unsplash API 的HTTP請求代碼在瀏覽器中,但在Java中的HTTP請求打開時,HTTP成功運行,我有這個問題java.io.FileNotFoundException爲unsplash API

我怎麼能解決這個問題

注:此代碼與Instagram的API成功運行

 new Thread() { 
      @Override 
      public void run() { 

       Log.i("ttttttrrrraaaaiiiinnn", "Getting access token"); 
       try { 
        URL url = new URL("https://api.unsplash.com/photos?client_id=***"); 
        Log.i("", "Send GET url liked image" + url.toString()); 
        HttpURLConnection urlConnection = (HttpURLConnection) url 
          .openConnection(); 
        urlConnection.setRequestMethod("POST"); 
        urlConnection.setDoInput(true); 
        OutputStreamWriter writer = new OutputStreamWriter(
          urlConnection.getOutputStream()); 
        writer.write("url"); 
        writer.flush(); 
        String response = Utils.streamToString(urlConnection 
          .getInputStream()); 
        Log.i("", "*?*?*?*?*?*?*?*?*?*?*? response *?*?*?*?*?*?*?*?*?*?*?" + response); 
        JSONObject jsonObj = (JSONObject) new JSONTokener(response) 
          .nextValue(); 

        tv.setText(jsonObj.toString()); 



       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 

      } 
     }.start(); 

錯誤

java.io.FileNotFoundException: https://api.unsplash.com/photos?client_id=**** 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:260) 
W/System.err:  at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 
W/System.err:  at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java) 
W/System.err:  at com.ksucs.graduationproject.myinterests.MainActivity$1.run(MainActivity.java:97) 

回答

0

restful api只支持GET訪問,你應該改變請求的方法/刪除setRequestMethod(...)的行,並且你不能寫一些東西給服務器,所以你必須刪除out變量和相對語句線。

urlConnection.setRequestMethod("GET");//by default is GET 
+0

非常感謝() –

相關問題