5

I am using below code to get versionName from playstore by using jsoup I am fetching details but its throwing some exception.

My code is

public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject>{

private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdateAsync(String currentVersion, Context context){
    this.currentVersion = currentVersion;
    this.context = context;
}

@Override
protected JSONObject doInBackground(String... params) {

    try {
         latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.getPackageName()+"&hl=en")
                .timeout(30000)
                .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                .referrer("http://www.google.com")
                .get()
                .select("div[itemprop=softwareVersion]")
                .first()
                 .ownText();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return new JSONObject();
}

@Override
protected void onPostExecute(JSONObject jsonObject) {
    if(latestVersion!=null){
        if(!currentVersion.equalsIgnoreCase(latestVersion)){
           // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
            if(!(context instanceof SplashActivity)) {
                if(!((Activity)context).isFinishing()){
                    showForceUpdateDialog();
                }
            }
        }
    }
    super.onPostExecute(jsonObject);
}

public void showForceUpdateDialog(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
            R.style.DialogDark));

    alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
    alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
            dialog.cancel();
        }
    });
    alertDialogBuilder.show();
}
}

but I am getting null pointer exception error

FATAL EXCEPTION: AsyncTask #1 Process: com.yabeee.yabeee, PID: 15893 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.ownText()' on a null object reference at com.yabeee.yabeee.ModelClasses.ForceUpdateAsync.doInBackground(ForceUpdateAsync.java:53) at com.yabeee.yabeee.ModelClasses.ForceUpdateAsync.doInBackground(ForceUpdateAsync.java:28) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:818)

Please some one help me to fix this issue.

Anil
  • 1,605
  • 1
  • 14
  • 24
  • most probably `.first()` is returning null can you check that? – Mohammad Tabbara Mar 16 '18 at 09:00
  • @MohammadTabbara yes its returning null how to fix – Anil Mar 16 '18 at 09:07
  • @MohammadTabbara i tried to log first its null can u please suggest me how to fix this – Anil Mar 16 '18 at 09:08
  • I felt that my answer is irrelevent so i deleted it. most probably google changed their div structure check the last comment of the accepted answer @ https://stackoverflow.com/questions/34309564/how-to-get-app-market-version-information-from-google-play-store for example softwareVersion could be another key now try currentVersion – Mohammad Tabbara Mar 16 '18 at 10:05
  • @MohammadTabbara thank u prob is my app still in beta version if i moved it to production it may get fixed i tried with some other apps its working fine – Anil Mar 16 '18 at 10:17
  • Instead of trying to select an element that clearly isn't in the HTML, why don't you use `...get().outerHtml()` to analyze the HTML so you know what to select? Then if you still have trouble post the HTML here, it is hard to help without know what the HTML looks like. – Developer Guy Mar 16 '18 at 12:20

2 Answers2

7

With updated code:

 newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + mCom.getPackageName()+ "&hl=en")
                            .timeout(30000)
                            .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                            .referrer("http://www.google.com")
                            .get()
                            .select("div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)")
                            .first()
                            .ownText();
Sreekanth
  • 407
  • 4
  • 8
  • 20
3

I used the same code and got the same error as well. I solved this error by taking help of web developer for better understanding of google play store updated code. use below code its working fine(code updated 10/08/2018)

public class ForceUpdateAsync extends AsyncTask {

    private String latestVersion;
    private String currentVersion;
    private Context context;
    public ForceUpdateAsync(String currentVersion, Context context){
        this.currentVersion = currentVersion;
        this.context = context;
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        try {
            latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + context.getPackageName()+ "&hl=en")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select("div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)")
                    .first()
                    .ownText();
            Log.e("latestversion","---"+latestVersion);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if(latestVersion!=null){
            if(!currentVersion.equalsIgnoreCase(latestVersion)){
                // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
                if(!(context instanceof SplashActivity)) {
                    if(!((Activity)context).isFinishing()){
                        showForceUpdateDialog();
                    }
                }
            }
        }
        super.onPostExecute(jsonObject);
    }

    public void showForceUpdateDialog(){

        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
    }

}
Vikram Kaldoke
  • 150
  • 1
  • 8
  • select("div.hAyfc:nth-child(3) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)") I haven't got latestVersion value (got No of download count) I need latest version can you share select command. – Dheerendra Mitm Aug 10 '18 at 06:51
  • try this one div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1) – Vikram Kaldoke Aug 10 '18 at 08:29