【问题标题】:java.io.FileNotFoundException for unsplash APIunsplash API 的 java.io.FileNotFoundException
【发布时间】:2017-03-17 13:53:54
【问题描述】:

这是我的 unsplash API HTTP 请求代码 HTTP 在浏览器中打开时成功运行,但在 java 中的 HTTP 请求中,我遇到了这个问题

我该如何解决这个问题

注意:此代码使用 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)

【问题讨论】:

    标签: java api httprequest


    【解决方案1】:

    restful api 只支持GET 访问,你应该改变请求的方法/删除setRequestMethod(...)的行。你不能写东西到服务器,所以你必须删除@987654322 @ 变量和相关语句行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多