How do I use the unofficial Android Market API?

三世轮回 提交于 2019-11-29 18:28:55

问题


I'm trying the sample code from here. But my app is crashing.

I added logging and found out that it's crashing at session.flush(); so I removed that line and it doesn't crash anymore.

But it doesn't reach the onResult callback.

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 = "somebody@gmail.com";
        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();                        
    }
}

回答1:


There is a problem with androidId. Instead of:

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

Use this:

String AndroidId = "dead000beef";

It Works.




回答2:


I strongly suggest to take a look at https://groups.google.com/forum/#!forum/android-market-api (the only place I know still being active about Android Market API).

Please take in account that the authentication method (login/pwd) is more than deprecated now (and not secure), and might not be anymore supported by the current market protocol.

Also a valid android id is not anymore as simple as before to retreive, see the groups for that too.




回答3:


This is not Secure.ANDROID_ID, it's Gtalk service device ID.

You can use the following code:

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;
    }
}

And add the permission to read Gservice

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


来源:https://stackoverflow.com/questions/6944441/how-do-i-use-the-unofficial-android-market-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!