0

although the concern is somewhat related to null as the ownText() returns null that's fine and can be handle the main-concern is the approach used is not well enough i am checking div[itemprop=softwareVersion .i have change the version checking with the answer given below which is just checking the softwareVersion for version on the playstore .

Original Question I have a live app which was working fine few days back ,now have a concern as when the user open app from the Play store or from app drawer or from home screen shortcut .app is getting crashed randomly and out 1/5 it is launching fine

Below is the version checker code

build.gradle(app level)

compile 'org.jsoup:jsoup:1.8.3'

java code:

public class VersionChecker extends AsyncTask<String, String, String> {
    String newVersion;
    Context context;
    public VersionChecker(Context context) {
        this.context = context;
    }
    @Override
    protected String doInBackground(String... params) {
        try {
            newVersion = 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 newVersion;
    } }

and below is the exception trace

Exception: FATAL EXCEPTION: main Process: com.demo.android.myapp, PID: 5325 java.lang.RuntimeException: Unable to resume activity {com.demo.android.myapp/com.demo.android.myapp.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference at com.demo.android.myapp.SplashActivity.checkVersion(SplashActivity.java:81) at com.demo.android.myapp.SplashActivity.onResume(SplashActivity.java:242) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) at android.app.Activity.performResume(Activity.java:6327) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Shekhu
  • 1,998
  • 5
  • 23
  • 49
  • add String newVersion="". some times you won't connect to internet, that time newVersion will be null or check neqVersion is null or not before checking – Vinayak B Mar 23 '18 at 07:08
  • put your SplashActivity code – Manthan Patel Mar 23 '18 at 07:20
  • nothing much in SplashActivity as primary concern is with version checker BTW i am using `VersionChecker versionChecker=new VersionChecker(); latestVersion = versionChecker.execute().get();`@ManthanPatel and @ Vinayak B null case is handled as `if (newVersion != null) return newVersion; else return CommonUtilities.getAppVersionName(context);` – Shekhu Mar 23 '18 at 07:24
  • Duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Mar 23 '18 at 07:35
  • text your `version name` here (old and current one). – Radhey Mar 23 '18 at 08:19
  • This code is working only in the case of 1.1,...1.9 ,2.0,2.1 ... .etc not in case of 1.1.9 , 2.0.0...make sure that things. – Radhey Mar 23 '18 at 08:21

1 Answers1

0

I had faced the same issue and found the solution is working fine for me:

build.gradle(app level)

 compile 'com.squareup.okhttp:okhttp:2.5.0'

java code:

public class VersionChecker extends AsyncTask<String, String, String> {
String newVersion;
Context context;
public VersionChecker(Context context) {
     this.context = context;
}
@Override
protected String doInBackground(String... params) {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url("https://play.google.com/store/apps/details?id=" + context.getPackageName() + "&hl=en")
            .build();
    try {
        Response response = client.newCall(request).execute();
        result = response.body().string();
    if(result!=null && result.contains("softwareVersion")){
        String parts[] = result.split("softwareVersion");
        result= parts[1].substring(
                parts[1].indexOf('>') + 1, parts[1].indexOf('<')
        ).trim();
    }else {
        result=CommonUtilities.getAppVersionName(context);
    }
    } catch (Exception ex) {
        result=CommonUtilities.getAppVersionName(context);
        ex.printStackTrace();
    }
    return result;
}}