使用Java';无法从Flickr API获取结果;s HttpURLConnection

使用Java';无法从Flickr API获取结果;s HttpURLConnection,java,android,rest,httpurlconnection,flickr,Java,Android,Rest,Httpurlconnection,Flickr,我在查询搜索照片时遇到问题: https://www.flickr.com/services/api/explore/flickr.photos.search 我正在使用HttpURLConnection在Android上编写一个小测试应用程序,我不想使用任何框架(如OkHttp等),因为这只是一个简单的学习练习 我遇到的问题是,尽管我的HttpURLConnection返回一个200,但没有JSON响应。但当我从日志中获取构造的REST URL时,例如: 并将其复制粘贴到浏览器中,我可以看

我在查询搜索照片时遇到问题:

https://www.flickr.com/services/api/explore/flickr.photos.search
我正在使用HttpURLConnection在Android上编写一个小测试应用程序,我不想使用任何框架(如OkHttp等),因为这只是一个简单的学习练习

我遇到的问题是,尽管我的HttpURLConnection返回一个200,但没有JSON响应。但当我从日志中获取构造的REST URL时,例如:

并将其复制粘贴到浏览器中,我可以看到正确的JSON

这是浏览器返回的JSON的一个片段:

{“照片”:{“页面”:1,“页面”:2614,“每页”:100,“总计”:“261380”,“照片”:[{“id”:“39168832065”,“所有者”:”23023080@N02“,”机密“:”1fb9ee772a“,”服务器“:”4615“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{”id“:”26194325488“,”所有者“:”23023080@N02“,”机密“:”3bc00e37ee“,”服务器“:”4656“,”农场“:”5,“标题“:”MG_2719_编辑”,“ispublic”:1,“isfriend”:0,“isfamily”:0},{“id”:“39168829925”,“所有者”:23023080@N02“,”秘密“:”15580858ce“,”服务器“:”4695“,”农场“:”5,“标题“,”编辑“,”ispublic“:”1,“isfriend“:0,“isfamily“:0},{”id“:”26194323798“,”所有者“:”23023080@N02“,”机密“:”c5e445bd6e“,”服务器“:”4743“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{“id”:“39168828415”,“所有者”:23023080@N02“,”秘密“:”89b54b28bc“,”服务器“:”4648“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{”id“:”26194322658“,”所有者“:”23023080@N02“,”秘密“:”7eff365389“,”服务器“:”4648“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend“:0,“isfamily“:0},{”id“:”39168826725“,”所有者“:”23023080@N02"“秘密”:“2eda660b60”,“服务器”:“4723”,“农场”:5,“标题”:“编辑”,“ispublic”:1,“isfriend”:0,“isfamily”:0},{“id”:“26194321108”,“所有者”:23023080@N02“,”机密“:”0c794e38a8“,”服务器“:”4765“,”农场“:5,“标题“,”编辑“,”ispublic“:”1,“isfriend“:0,“isfamily“:0},{”id“:”40034887842“,”所有者“:”23023080@N02“,”机密“:”6269997f0f“,”服务器“:”4755”,“农场”:5,“标题”:“编辑”,“ispublic”:1,“isfriend”:0,“isfamily”:0},{“id”:“26194319778”,“所有者”:”23023080@N02“,”秘密“:”32991c9151“,”服务器“:”4763“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{”id“:”26194319168“,”所有者“:”23023080@N02,“机密”:“4dd5555f5c”,“服务器”:“4709”,“农场”:5,“标题”:“编辑”ispublic“:1,“isfriend“:0,“isfamily“:0},{”id:“40034885802”,“所有者”:”23023080@N02“,”机密“:”3f9c5031db“,”服务器“:”4671“,”农场“:5,“标题“,”编辑“,”ispublic“:”1,“isfriend“:0,“isfamily“:0},{”id“:”26194317818“,”所有者“:”23023080@N02“,”机密“:”e20b503f8f“,”服务器“:”4668“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{”id:“40067785161”,“所有者”:23023080@N02“,”机密“:”4ab87667fd“,”服务器“:”4612“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{”id“:”40034884132“,”所有者“:”23023080@N02“,”机密“:”235b7dc32d“,”服务器“:”4744“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},{”id“:”40067784231“,”所有者“:”23023080@N02","秘密“:”0bbae58322“,”服务器“:”4622“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend“:0,“isfamily“:0},{”id“:”40034882942“,”所有者“:”23023080@N02“,”机密“:”30345b6b7b“,”服务器“:”4626“,”农场“:5,“标题“,”编辑“,”ispublic“:1,“isfriend”:0,“isfamily”:0},

为什么代码没有从连接的输入流获取/读取JSON?

package com.bing.ary.xyz;

import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;


public class SearchFlickerAsync extends AsyncTask<String, Void, flikrPhotosResponse> {

        private static final String TAG = "SearchFlickerAsync";

        private String query;

        public SearchFlickerAsync(String query) {
        this.query = query;
        }

    public String getQuery() {
        return query;
        }

        public void setQuery(String query) {
        this.query = query;
        }

    @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected flikrPhotosResponse doInBackground(String... query) {

            Log.i(TAG,"doInBackground(), SearchFlickerAsync input param:"+query);

            HttpsURLConnection httpURLConnection = null;
            flikrPhotosResponse FlikrPhotosResponse = null;
            JsonElement jsonElemnt = null;
            JsonObject jsonObject = null;
            String queryUrl = null;
            StringBuilder queryBuilder = null;

            try {
                //create url string
                queryBuilder = new StringBuilder();
                queryBuilder.append(flickerApi.baseUrl);
                queryBuilder.append(flickerApi.searchFlicker);
                queryBuilder.append(flickerApi.flickrQuery_key + flickerApi.Key);
                queryBuilder.append(flickerApi.flickrQuery_tag);
                queryBuilder.append(this.query);
                queryBuilder.append("&format=json");
                queryBuilder.append("&nojsoncallback=1&api_sig=be97f5275f5a128fbe69dc2cde2560b9");
                //instantiate url for connection
                URL url = new URL(queryBuilder.toString());
                httpURLConnection = (HttpsURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setUseCaches(false);
                httpURLConnection.setAllowUserInteraction(false);
                httpURLConnection.setRequestProperty("Content-length", "0");
                httpURLConnection.connect();
                //no authorization token needed public API httpURLConnection.setRequestProperty("Authorization", "");
                Log.i(TAG,"doInBackground(), http request:"+url.toString());
                int status = httpURLConnection.getResponseCode();

                switch (status) {
                    case 200:
                    case 201:
                        Log.i(TAG,"doInBackground(), http response status:"+status);
                        BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                        StringBuilder sb = new StringBuilder();
                        String line;
                        while ((line = br.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        Log.i(TAG,"doInBackground(), http response line:"+line);
                        jsonElemnt = new JsonParser().parse(sb.toString());
                        jsonObject = jsonElemnt.getAsJsonObject();
                        /*Further parse & instantiate FlikrPhotosResponse = */
                        br.close();
                        break;
                    //TODO: ERRORS https://www.flickr.com/services/api/flickr.groups.search.html
                    default:
                        Log.e(TAG,"doInBackground(), http response error status:"+status);
                        //TODO: handle error responses
                        break;

                }



            } catch (IOException ex) {

                ex.printStackTrace();

            } catch (Exception ex) {

                ex.printStackTrace();

            } finally {
                if (httpURLConnection != null) {
                    try {
                        httpURLConnection.disconnect();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }

            return FlikrPhotosResponse;
        }//end doInBackgroud

        protected void onPostExecute(flikrPhotosResponse FlikrPhotosResponse) {

            Log.i(TAG,"onPostExecute(), FlikrPhotosResponse:"+FlikrPhotosResponse);

        }


    }//end class

我还

您记录了错误的内容:

while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
}
Log.i(TAG,"doInBackground(), http response line:"+line);
当然,
line
此时为空。这就是循环终止的原因。您应该记录
sb.toString()

注意:

  • 您不需要以下任何一项:

    httpURLConnection.setRequestMethod("GET");
    httpURLConnection.setRequestProperty("Content-length", "0");
    httpURLConnection.connect();
    
  • 您需要使用
    HttpsURLConnection
    的注释中的大量声明是不正确的。使用
    HttpURLConnection
    是完全有效的,即使URL是HTTPS URL,只要您不需要
    HttpsURLConnection
    的额外方法,因为
    HttpsURLConnection
    扩展了
    HttpURLConnectione> 


  • 我想你需要
    HttpsURLConnection
    而不是
    HttpURLConnection
    。还没有确认,所以只是作为评论发布…@Alexander,构建的URl已经是https了,请仔细阅读帖子:…我想你误解了。你的代码是通过非https类进行通信的。请将你的logcat输出添加到问题中很多c对于一个请求,Commercial端点将以200作为响应,但仅通过适当的ssl握手提供实际货物。您是否尝试使用“s”调用该类?因为它是Flickr,所以您无需担心将其证书添加到您的信任存储或任何类似复杂的内容,因为它将被合法签名。
    httpURLConnection.setRequestMethod("GET");
    httpURLConnection.setRequestProperty("Content-length", "0");
    httpURLConnection.connect();