Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Blogger API调用返回401错误Android_Android_Json_Blogger - Fatal编程技术网

Blogger API调用返回401错误Android

Blogger API调用返回401错误Android,android,json,blogger,Android,Json,Blogger,我试图在Android应用程序中检索博主帖子的JSON列表,但结果我一直得到这个JSON: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" }

我试图在Android应用程序中检索博主帖子的JSON列表,但结果我一直得到这个JSON:

{
"error": {
"errors": [
{
 "domain": "global",
"reason": "required",
 "message": "Login Required",
 "locationType": "header",
 "location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
下面是我的api调用的格式

BLOG_ID和API_KEY是代码中使用的实际值的占位符

下面是检索JSON的代码

private class AsyncCaller extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub



        DefaultHttpClient httpclient = new DefaultHttpClient(
                new BasicHttpParams());
        HttpPost httppost = new HttpPost(Constants.BLOOGER_API_REQUEST_PREFIX
                + "/blogs/" + Constants.BLOGGER_TEST_BLOGID + "/posts?key="
                + Constants.GOOGLE_API_KEY);

        Log.d("HTTPPOST", Constants.BLOOGER_API_REQUEST_PREFIX
                + "/blogs/" + Constants.BLOGGER_TEST_BLOGID + "/posts?key="
                + Constants.GOOGLE_API_KEY);

        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = "";
        Log.d("BEFORE TRY", "BEFORE TRY");
        try {
            HttpResponse response = httpclient.execute(httppost);
            Log.d("Executed HTTP POST", "EXECUTED HTTPPOST");
            HttpEntity entity = response.getEntity();
            Log.d("GOT ENTITY", "GOT ENTITY");
            inputStream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;

            Log.d("INSIDE TRY", "INSIDE TRY");
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
                Log.d("IN LOOP", "IN LOOP");
            }
            result = sb.toString();

            Log.d("RESULT DONE", result.toString());


        } catch (Exception e) {
            // Bad
            Log.d("BAD E", "BAD E");
            e.printStackTrace();

        } finally {

            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception squish) {
            }

        }

        if(result != null){

        Log.d("RESULT GOOD", result.toString());
        } else {
            Log.wtf("BAD", "NULL RESULT");

        }

        onPostExecute(result);
    return result;
}
私有类AsyncCaller扩展AsyncTask{
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
DefaultHttpClient httpclient=新的DefaultHttpClient(
新的BasicHttpParams());
HttpPost HttpPost=新的HttpPost(Constants.BLOOGER\u API\u REQUEST\u前缀
+“/blogs/”+Constants.BLOGGER\u TEST\u BLOGID+”/posts?key=”
+常量。GOOGLE_API_密钥);
Log.d(“HTTPPOST”,Constants.BLOOGER\u API\u请求\u前缀
+“/blogs/”+Constants.BLOGGER\u TEST\u BLOGID+”/posts?key=”
+常量。GOOGLE_API_密钥);
setHeader(“内容类型”、“应用程序/json”);
InputStream InputStream=null;
字符串结果=”;
Log.d(“试前”、“试前”);
试一试{
HttpResponse response=httpclient.execute(httppost);
Log.d(“执行的HTTP POST”、“执行的HTTPPOST”);
HttpEntity=response.getEntity();
Log.d(“GOT实体”、“GOT实体”);
inputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
输入流,“UTF-8”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
Log.d(“内部尝试”、“内部尝试”);
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
Log.d(“循环中”、“循环中”);
}
结果=sb.toString();
Log.d(“结果完成”,RESULT.toString());
}捕获(例外e){
//坏的
日志d(“坏E”、“坏E”);
e、 printStackTrace();
}最后{
试一试{
如果(inputStream!=null){
inputStream.close();
}
}捕获(异常挤压){
}
}
如果(结果!=null){
Log.d(“结果良好”,RESULT.toString());
}否则{
Log.wtf(“坏”、“空结果”);
}
onPostExecute(结果);
返回结果;
}
如您所见,我正在使用API密钥对请求进行签名,因此这不是问题所在

我已经进入了有问题的博客的设置,并将可见性设置为public,因此我认为问题不在于OAuth。我不知道还需要什么来实现“需要登录”在应用程序中,我更希望用户能够打开应用程序并查看博客帖子,而不必暴露在幕后。而我的https、api调用在浏览器中工作得非常好,为我提供了我所期望的确切结果列表,因此api调用的语法似乎不是问题所在她,这让我不知所措。

使用httpGet方法

      HttpGet httpGet = new HttpGet("https://www.googleapis.com/blogger/v3/blogs/BLOG_ID/posts?key=" +paramAPIKEY);

只需将postmethod替换为Get

您在浏览器中尝试过吗?是否获得了所需的结果result@Indra它在浏览器中工作得非常好。它的Get方法或post方法这是一个Get方法调用。您在代码中使用了post方法。我误解了您之前的评论,很抱歉,谢谢您的帮助。祝您编码愉快:)