2011-08-04 30 views
9

我在嘗試從here的示例代碼。但是我的應用崩潰了。如何使用非官方的Android Market API?

我添加了日誌記錄,發現它崩潰在session.flush();,所以我刪除了該行,它不再崩潰。

但它沒有達到onResult的回調。

package com.mytest.app; 

import com.gc.android.market.api.MarketSession; 
import com.gc.android.market.api.MarketSession.Callback; 
import com.gc.android.market.api.model.Market.AppsRequest; 
import com.gc.android.market.api.model.Market.AppsResponse; 
import com.gc.android.market.api.model.Market.ResponseContext; 

import android.app.Activity; 
import android.os.Bundle; 
import android.provider.Settings.Secure; 
import android.util.Log; 

public class MarketAPITestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.d("Market API", "Started"); 

     String email = "[email protected]"; 
     String pass = "mypass"; 
     String AndroidId = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID); 

     MarketSession session = new MarketSession(); 
     session.login(email,pass); 
     session.getContext().setAndroidId(AndroidId); 

     String query = "maps"; 
     AppsRequest appsRequest = AppsRequest.newBuilder() 
             .setQuery(query) 
             .setStartIndex(0).setEntriesCount(10) 
             .setWithExtendedInfo(true) 
             .build(); 

     session.append(appsRequest, new Callback<AppsResponse>() { 
       @Override 
       public void onResult(ResponseContext context, AppsResponse response) { 
         Log.d("Market API", "Got response"); 
       } 
     }); 

     session.flush();       
    } 
} 
+0

您可以發佈您的代碼?還有什麼例外? – Jack

+0

用代碼更新了問題。我試圖用try/catch來捕獲異常,但eclipse說它無法到達,所以我不得不刪除它。我不確定在哪裏添加try/catch。 – mrburns

+0

你能發佈異常嗎?如果你看看你的logcat,它應該在session.flush()上顯示一個異常。 – Jack

回答

1

我強烈建議看一看https://groups.google.com/forum/#!forum/android-market-api(只有我自己知道仍被關於Android Market API活躍的地方)。

請注意,身份驗證方法(login/pwd)現在已不再被棄用(並且不安全),並且可能不再受當前市場協議的支持。

另外一個有效的android id不再像以前那樣簡單了,請參閱這個組。

4

androidId有問題。相反的:

String AndroidId = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID); 

使用此:

String AndroidId = "dead000beef"; 

它的作品。

1

這不是Secure.ANDROID_ID,它是Gtalk服務設備ID。

您可以使用下面的代碼:

public String getDeviceId(Context context) { 
    String[] params = { GSERVICES_ID_KEY }; 
    Cursor c = context.getContentResolver() 
      .query(GSERVICES_URI, null, null, params, null); 

    if (!c.moveToFirst() || c.getColumnCount() < 2) 
     return null; 

    try { 
     return Long.toHexString(Long.parseLong(c.getString(1))).toUpperCase(); 
    } catch (NumberFormatException e) { 
     return null; 
    } 
} 

並添加權限讀取Gservice

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
+1

GSERVICES_ID_KEY和GSERVICES_URI的價值是什麼 – Vamsi

相關問題