【问题标题】:How to Intregrate Yammer api in android如何在 Android 中集成 Yammer api
【发布时间】:2013-05-24 07:57:07
【问题描述】:

我在 Android 中遇到 Yammer 集成问题,我无法使用 Yammer 对我的应用进行身份验证,因此我无法通过我的 Android 应用在 Yammer 上发布我的数据。

JSONObject job = new JSONObject();
/*ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("score", score));          // user : User name from Text Field
*/
HttpPost post = new HttpPost("https://www.yammer.com/api/v1/messages.json?client_id=JV8Vr6vYaF0RdyVnLKhnRg&client_secret=zACHEVUnUKaRD58Ho5MvnSjvRZaadNqpCOWirc9I8SiA&access_token="+tokens[1]); 
System.out.println("tokens[1]----------.>>>>>"+tokens[1]);

post.setHeader("Content-Type", "text/json; charset=utf-8"); // Header
            // for
            // HttpPost
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                public String handleResponse(HttpResponse response) // Header
                // for
                // HttpResponse
                        throws ClientProtocolException, IOException {
                    response.setHeader("Content-Type", "charset=utf-8");
                    HttpEntity entity = response.getEntity();
                    StringBuffer outString = new StringBuffer();
                    outString.append(EntityUtils.toString(entity));
                    return outString.toString();
                }
            };
            try {
                System.out.println("hi this is deloitte game.");
                post.setEntity(new StringEntity("hi this is deloitte game."));
                DefaultHttpClient httpclient = new DefaultHttpClient();
                String response = httpclient.execute(post, responseHandler);
                System.out.println("response---->>"+response);
                //job = new JSONObject(response);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*

我已将这些代码用于 yammer 集成,在此代码中,我使用 post 方法将数据发布到 yammer 上,但无法发布到 yammer 墙上。

【问题讨论】:

  • 我的意思是,从你的代码来看,很多事情都可能出错。无法按原样回答您的问题。如果有,您必须 A/ 发布堆栈跟踪。 B/ 如果有,请发布您从 yammer 收到的错误。
  • 我没有收到任何错误,当我通过此代码从我的应用程序登录我的帐户时,它应该在我的墙上发布一个数据,但它现在没有发布......如果你有任何其他代码请给我...
  • 服务器的回答是什么? (并且您不能在响应中设置Header,标头是由服务器设置的,因为就像 知道他在响应中放入了什么,而不是您)
  • 你应该测试 response.getStatusLine().getStatusCode()。
  • response.getStatusLine().getStatusCode() 给我 getStatusLine() 未定义的错误。

标签: android api yammer


【解决方案1】:

根据 Yammer 文档https://developer.yammer.com/restapi/#rest-messages

您应该将 parameters 传递给请求,而不是帖子中的纯字符串。 (否则,你怎么指望 Yammer 知道正文是什么,主题是什么,附件是什么……)

通常,这是使用带有NameValuePair 列表的UrlEncodedFormEntity 完成的。

您的实体应如下所示:

List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new BasicNameValuePair("body", "hi this is deloitte game.");
HttpEntity postEntity = new UrlEncodedFormEntity(values);
post.setEntity(postEntity);

服务实际上可能需要一个 JSON 输入,在这种情况下甚至很简单。

编辑

如果它在 json 中,我可能看起来像:

JSONObject jsonObject = new JSONObject();
jsonObject.put("body", "hi this is a body");
post.setEntity(new StringEntity(jsonObject.toString()));

【讨论】:

  • 我已将您的代码包含在我的课堂上,但我仍然得到相同的响应 05-24 10:17:07.048: I/System.out(6383): response---->>{" body":["请附上信息"]}
  • 我刚刚在文档POST messages are form encoded. 中找到。所以我给出的第一个解决方案应该有效。您可能需要更改请求内容类型以匹配此内容。另外,我不确定您是否需要秘密和拥有令牌的 id
  • 谢谢@njzk2 我已经解决了我的 yammer 问题,问题是按照您所说的设置标题。我已经更改它并且它有效...再次感谢...
猜你喜欢
  • 2020-01-27
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
相关资源
最近更新 更多