将JSON数据发送到Google Plus

时间:2013-05-31 03:55:11

标签: android json google-plus

我正在尝试向Google+发送JSON对象和相关缩略图。但我无法让它发挥作用。例如,我得到了答复:

05-30 22:38:16.819: E/AndroidRuntime(11643): FATAL EXCEPTION: main
05-30 22:38:16.819: E/AndroidRuntime(11643): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=application/json flg=0x80000 pkg=com.google.android.apps.plus (has extras) }
05-30 22:38:16.819: E/AndroidRuntime(11643):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)

告诉我Google+图书馆找不到处理“application/json”MIME类型的正确方法。我的代码(相关部分)像这样运行(我从the Google+ examples获得了大部分内容):

PlusShare.Builder builder = new PlusShare.Builder(this, plusClient);

// Set call-to-action metadata.
builder.addCallToAction("VIEW_ITEM", callToActionUrl, callToActionDeepLinkId);
File thumbnailFile = generateThumbnail();
builder.setStream(Uri.fromFile(outputJSON()));
builder.setType("application/json");
thumbnailFile!=null?Uri.fromFile(thumbnailFile):null);

如果我避免将流设置为JSON类型,它似乎正常工作。我正在生成的JSON是这样的:

{"INSTRUMENTS":
      [{"MINOR":false,"CHANNEL":0,"MAJOR":false,"HIGH_RANGE":-8012206,
        "PROGRAM":1,"MAX_KEY":70,"NOTE_LENGTH":150,"LOW_RANGE":-16217748,
        "MIN_VELOCITY":60,"MIN_KEY":40},
       {"MINOR":false,"CHANNEL":2,"MAJOR":true,"HIGH_RANGE":-2790500,
        "PROGRAM":8,"MAX_KEY":90,"NOTE_LENGTH":150,"LOW_RANGE":-12114977,
        "MIN_VELOCITY":60,"MIN_KEY":70}]}

我已经看到了不同的API,告诉我们如何使用Bitmaps发送像这样的JSON对象,但是你有什么东西,但Android API文档略微......稀疏。谁知道我怎么能在Android中做同样的事情呢?

理想情况下,一旦正确完成,帖子应包含:

  1. 图像的指纹
  2. 当用户点击Android设备上的帖子时,我的应用会通过深层链接使用JSON数据
  3. 用户撰写的文字/标题

2 个答案:

答案 0 :(得分:2)

您无法将图片或JSON数据附加到互动帖子。所以你的问题没有简单的答案。

一种选择是使用普通帖子而不是互动帖子来共享您的缩略图 - http://googleplusplatform.blogspot.co.uk/2012/05/sharing-rich-content-from-your-android.html,但您也无法将JSON数据附加到此帖子。

使用互动帖子的第二种替代方法是设置公开可见的网址,该网址是builder.setContentUrl(Uri)帖子的内容。如果您创建包含以下内容的页面,例如:

<body itemscope itemtype="http://schema.org/WebPage">

<div itemscope itemtype="http://schema.org/Thing">

<img itemprop="image" src="http://example.com/path/to/thumbnail.jpg" />
<span itemprop="name">Name of your thing</span> 

<div class="data">{"INSTRUMENTS":
  [{"MINOR":false,"CHANNEL":0,"MAJOR":false,"HIGH_RANGE":-8012206,
    "PROGRAM":1,"MAX_KEY":70,"NOTE_LENGTH":150,"LOW_RANGE":-16217748,
    "MIN_VELOCITY":60,"MIN_KEY":40},
   {"MINOR":false,"CHANNEL":2,"MAJOR":true,"HIGH_RANGE":-2790500,
    "PROGRAM":8,"MAX_KEY":90,"NOTE_LENGTH":150,"LOW_RANGE":-12114977,
    "MIN_VELOCITY":60,"MIN_KEY":70}]}</div>

</div>
</body>

并在http://example.com/item1处提供,然后您就可以创建这样的互动帖子:

PlusShare.Builder builder = new PlusShare.Builder(this, plusClient);

// Set call-to-action metadata.
builder.setContentUrl(Uri.parse("http://example.com/thing1"));
builder.addCallToAction("VIEW_ITEM", Uri.parse("http://example.com/thing1"), deepLinkId);

但这意味着您必须加载并解析页面以检索您的JSON数据,这可能更好地托管在不同的URL上。

您可以将您的JSON数据放在deepLinkId本身,但要注意deepLinkId限制为512个字符,因为它不用于携带数据 - 仅用于标识资源。

答案 1 :(得分:0)

Lee的另一种方法可能有效,但您可能会将JSON数据作为帖子的深层链接ID传递。您可能希望对数据进行base64编码,将其用作深层链接ID,然后在捕获传入的深层链接时,对数据进行解码并解析。

您必须确保编码的JSON数据保持在512个字符以下。您编写的当前JSON代码段为524个字符。

byte[] data = jsonData.getBytes("UTF-8");
String base64Json = Base64.encodeToString(data, Base64.DEFAULT);

if (base64.length() <= 512) {
Intent shareIntent = new PlusShare.Builder(this)
        .setText("Lemon Cheesecake recipe")
        .setType("text/plain")
        .setContentDeepLinkId(base64Json, /** Deep-link identifier */
                "Lemon Cheesecake recipe", /** Snippet title */
                "A tasty recipe for making lemon cheesecake.", /** Snippet description */
                Uri.parse("http://example.com/static/lemon_cheesecake.png"))
        .getIntent();

startActivityForResult(shareIntent, 0);
} else {
  // Do something different such as regular share, or try to get encoded length under 512
}

对于应用程序的传入链接,您将读取此数据的解码并使用它。您需要非常小心验证数据。