【问题标题】:Execute HttpPost with google token in android is not fetching JSON data在 android 中使用 google 令牌执行 HttpPost 未获取 JSON 数据
【发布时间】:2014-09-22 23:49:11
【问题描述】:
HttpPost httpPost = new HttpPost("MyWebsiteURL");
        try {
            httpPost.setEntity(new StringEntity("https://www.googleapis.com/plus/v1/people/me?key="+token));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String json = sb.toString();
            Log.i("JSON", json);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

【问题讨论】:

  • 我认为我们不能使用 post 方法在 URL 中传递参数..
  • @Prag's 那么如何使用谷歌令牌与我的网站建立连接?
  • @Prag 很抱歉.. URL 类似于 Mywebsite/auth/auth2_login
  • 使用 BasicNameValuePair 传递令牌
  • @Prag's 这是 BasicNameValuePair 的语法 --- 它有两个参数,我如何只传递令牌。 List 对 = new ArrayList(); pair.add(new BasicNameValuePair("key1", "value1")); post.setEntity(new UrlEncodedFormEntity(pairs));

标签: android http-post google-oauth access-token httpentity


【解决方案1】:

这是用于身份验证的帖子。这将返回一个包含访问令牌的 HttpResponse 当您执行 HttpGet 请求时,您将在其中使用。

  try {
        Log.i(tag, "Starting doHTTPPost");
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("KEY", "VALUE"));

        /* EXAMPLE of pairs */
        pairs.add(new BasicNameValuePair("client_id","theGreatSecret12345"));
        pairs.add(new BasicNameValuePair("username", "purple"));
        pairs.add(new BasicNameValuePair("password", "asjdf098732hkndfa"));

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("The API Server you want to access");
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        HttpResponse apiResponse = response;
        String resultFromServerAsAString = EntityUtils.toString(response.getEntity());

        Log.i(tag, "Response statusCode : "+response.getStatusLine().getStatusCode());
        Log.i(tag, "Response StatusLine : "+response.getStatusLine());
        Log.i(tag, "Ending doHTTPPost");

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【讨论】:

    【解决方案2】:

    POSTGET 方法之间存在差异...

    在 GET METHOD 中,您可以使用 URL 传递数据....
    但是在 POST 方法中,您不能使用 URL 传递数据……您必须将其作为实体传递………… 为将数据传递到 URL 执行以下操作

    试试这个..

          DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httpPost = new HttpPost(WEBSITE_URL);
            try{    
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
                //use this to pass variables while using POST method
                // add an HTTP variable and value pair
                nameValuePairs.add(new BasicNameValuePair("key name","key value")); 
                nameValuePairs.add(new BasicNameValuePair("key name","key value"));
                nameValuePairs.add(new BasicNameValuePair("key name","key value"));
    
                // passing data to the URL
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
                //do the following..
    
               }
    

    为你的解决方案你应该通过

    nameValuePairs.add(new BasicNameValuePair("key",token));
    

    在哪里,

     Key - variable or key you defined in Your page (Backend side)     
     token = is a value which you want to pass..........
    

    【讨论】:

      【解决方案3】:

      首先你不能在http post中传递这样的参数,使用下面的代码供你使用,可能会有一些编译错误,因为我没有使用和IDE检查正在发布的代码,重点是告诉你如何传递post使用http post的参数,即使用NameValuePairs,请相应调整您的url

              try {
                 HttpPost httppost = new HttpPost("https://www.googleapis.com/plus/v1/people/me");
                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                 nameValuePairs.add(new BasicNameValuePair("key", "12345"));
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
                  HttpResponse httpResponse = httpClient.execute(httpPost);
                  HttpEntity httpEntity = httpResponse.getEntity();
                  InputStream is = httpEntity.getContent();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(
                          is, "iso-8859-1"), 8);
                  StringBuilder sb = new StringBuilder();
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
                  }
                  is.close();
                  String json = sb.toString();
                  Log.i("JSON", json);
      
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      

      【讨论】:

      • HttpPost 参数 = 代表 POST 目的地的字符串。我想向我的网站发出 HttpPost 请求。
      • new HttpPost("googleapis.com/plus/v1/people/me"),传递你想引用的任何网站,同时创建新的 HttpPost 对象。
      • 我对新的 HttpPost().setEntity() 参数有疑问。不与 HttpPost 对象。我想将这个 ("googleapis.com/plus/v1/people/me?key="+token) 发布到我的网站。
      • 那是想帮你,key是你的参数,我刚刚给你提过发帖的方式!!
      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 2013-02-12
      • 2015-06-06
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多