9

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();                        
    }
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
mrburns
  • 453
  • 1
  • 8
  • 17
  • Can you post your code? And any exceptions? – Jack Aug 04 '11 at 15:53
  • updated question with the code. I tried to catch the exception with a try/catch but eclipse said it couldn't be reached so I had to remove it. I'm not sure where to add the try/catch. – mrburns Aug 04 '11 at 15:58
  • Can you post the exception? If you look in your logcat, it should show an exception at session.flush(); – Jack Aug 04 '11 at 18:11
  • 08-04 23:44:14.389: ERROR/AndroidRuntime(11548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytest.app/com.mytest.app.MarketAPITestActivity}: java.lang.RuntimeException: java.lang.RuntimeException: Response code = 400, msg = Bad Request – mrburns Aug 04 '11 at 18:17
  • 3
    Okay, the problem has something to do with the android id. When I change the android id to "dead000beef", I get a valid response from the server. When I use my own androidid, I get a 400 error. – mrburns Aug 04 '11 at 19:00
  • Was the problem fixed mrburns? Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. – Zecas May 24 '12 at 17:23
  • It's issue of AndroidId.. have you solved i am also facing issue..dead000beef works but I have application particular for india which does not come in my searching app list. How to resolve have you solved? please help – Bhavesh Hirpara Apr 03 '13 at 04:33
  • This way is not effective. It is not an official API and it's developers can change or update several time, therefore you will need change your code or approach several time. I created an app with this unofficial API, first time it worked well, then after 1 month crashed, and again became working, then crashed again. And this API can retrieve information not about all applications on the Market. I think that the developers have their own database and they update it when they want. I decided to do not use this API. – Daryn Nov 22 '13 at 09:43
  • does anyone know if it is able to get top free apps? – Mustafa Güven May 04 '14 at 17:53
  • 2
    session.login always throw LoginException error. any help? – Vamsi Jan 13 '15 at 10:25

3 Answers3

4

There is a problem with androidId. Instead of:

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

Use this:

String AndroidId = "dead000beef";

It Works.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
i18n
  • 130
  • 5
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.

Aladin Q
  • 550
  • 5
  • 12
1

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"/>
binhgreat
  • 982
  • 8
  • 13