2013-05-31 45 views
-1

我想发送一个JSON对象和相关的缩略图到Google+。但是我无法使其正常工作。我得到的,例如,响应:发送JSON数据到谷歌Plus

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,告诉如何发送JSON对象像这样使用位图,什么具备的,你,但Android的API单证稍...疏。谁知道我可以如何设法在Android中做同样的事情?

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

  1. 的图像的指纹
  2. 当用户点击从Android设备后其,
  3. JSON数据,将被使用的我的通过用户
+0

你能发表更完整的代码吗?它不清楚你如何定义你的“建造者”变量。另外,你能否指出你希望得到的帖子看起来像 - 即。你打算如何处理application/json mime类型的Google+? – Lee

+0

@Lee:我添加了显示构建器实例的代码。我还补充说明了我正在努力完成的事情。 – DigCamara

回答

2

您不能附加图片或JSON数据的互动信息,写好深联

  • 文/标题的应用程序。所以你的问题没有简单的答案。

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

    使用交互式帖子的第二种替代方法是设置公开可见的URL,该帖子的内容是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个字符,因为它无意携带数据 - 仅用于识别资源。

  • +0

    是的......那是我认为我必须去的地方。你知道他们为什么有像'setStream()'和'setType()'这样的方法吗?他们对我来说似乎毫无用处。 – DigCamara

    +0

    我确定setStream和addStream旨在反映http://developer.android.com/reference/android/support/v4/app/ShareCompat.IntentBuilder.html中的使用情况,但它们似乎不起作用同样的方式。 – Lee

    +0

    似乎喜欢它。我认为你的回答是正确的,但由于没有很好的官方文件能够描述这种“真实”的方式,所以我将把问题留待一段时间。 – DigCamara

    0

    李的另一种方法可能会工作,但您可能会传递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 
    } 
    

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