android volley和asynchttpclient在真机上失败,但在模拟器上运行正常。

huangapple go评论70阅读模式
英文:

android volley and asynchttpclient failed on real device but worked on emulator

问题

以下是代码部分的翻译:

package com.orangevpninfo.orangevpn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import com.airbnb.lottie.LottieAnimationView;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.orangevpninfo.orangevpn.models.Version;
import cz.msebera.android.httpclient.Header;
import org.json.JSONArray;
import org.json.JSONObject;

public class SplashActivity extends AppCompatActivity {

    LottieAnimationView la_animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        la_animation=findViewById(R.id.la_animation2);
        startAnimation(SplashActivity.this, R.id.la_animation2, R.anim.fade_in_1000, true);
        la_animation.cancelAnimation();
        la_animation.setAnimation(R.raw.loading2);
        la_animation.playAnimation();

        checkupdate2("1");
    }

    private void download(final String Link){
        AlertDialog alertDialog = new AlertDialog.Builder(SplashActivity.this).create();
        alertDialog.setTitle("New Version Available");
        alertDialog.setMessage("There is a New version of this App! Please select Update to Download directly new version and install it");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Not Now",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Update",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent a=new Intent(Intent.ACTION_VIEW, Uri.parse(Link));
                        startActivity(a);
                        finish();
                    }
                });
        alertDialog.show();
    }

    private void checkupdate2(final String version){
        Request<JSONObject> jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://51.89.45.117/version/version.json",
                null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("Success", response.toString());
                Version ver=new Gson().fromJson(response.toString(), Version.class);
                if (ver.getVersion().equals(version)){
                    Intent a=new Intent(SplashActivity.this,MainActivity.class);
                    startActivity(a);
                    finish();
                }else{
                    download(ver.getLink());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error", error.toString());
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(jsonObjectRequest);
    }

    public void startAnimation(Context ctx, int view, int animation, boolean show) {
        final View Element = findViewById(view);
        if (show) {
            Element.setVisibility(View.VISIBLE);
        } else {
            Element.setVisibility(View.INVISIBLE);
        }
        Animation anim = AnimationUtils.loadAnimation(ctx, animation);
        Element.startAnimation(anim);
    }
}

Volley错误和AsyncHttpClient错误部分已被删除,以便提供正确的翻译。如果您需要这些错误的翻译,请告诉我。

英文:

hello every one I just make an android app that using ICS_OPENVPN library but when I make SplashActivity to check for update app worked Currect on emulator but when I generate for signed APK and install it on real device it is not switch to MainActivity.

SplashActivity.java

package com.orangevpninfo.orangevpn;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import com.airbnb.lottie.LottieAnimationView;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.orangevpninfo.orangevpn.models.Version;
import cz.msebera.android.httpclient.Header;
import org.json.JSONArray;
import org.json.JSONObject;
public class SplashActivity extends AppCompatActivity {
LottieAnimationView la_animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
la_animation=findViewById(R.id.la_animation2);
startAnimation(SplashActivity.this, R.id.la_animation2, R.anim.fade_in_1000, true);
la_animation.cancelAnimation();
la_animation.setAnimation(R.raw.loading2);
la_animation.playAnimation();
checkupdate2(&quot;1&quot;);
}
private void download(final String Link){
AlertDialog alertDialog = new AlertDialog.Builder(SplashActivity.this).create();
alertDialog.setTitle(&quot;New Version Available&quot;);
alertDialog.setMessage(&quot;There is a New version of this App! Please select Update to Download directly new version and install it&quot;);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, &quot;Not Now&quot;,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, &quot;Update&quot;,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent a=new Intent(Intent.ACTION_VIEW, Uri.parse(Link));
startActivity(a);
finish();
}
});
alertDialog.show();
//Intent i = new Intent(Intent.ACTION_VIEW);
//i.setData(Uri.parse(Link));
//startActivity(i);
}
private void checkupdate2(final String version){
Request&lt;JSONObject&gt; jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, &quot;http://51.89.45.117/version/version.json&quot;,
null, new Response.Listener&lt;JSONObject&gt;() {
@Override
public void onResponse(JSONObject response) {
Log.d(&quot;Success&quot;, response.toString());
Version ver=new Gson().fromJson(response.toString(), Version.class);
if (ver.getVersion().equals(version)){
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
finish();
}else{
download(ver.getLink());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(&quot;Error&quot;, error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
}
private void checkupdate(final String version){
String url=&quot;http://51.89.45.117/version/version.json&quot;;
AsyncHttpClient client = new AsyncHttpClient();
client.setConnectTimeout(30);
client.get(this,url,new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
Version[] ver=new Gson().fromJson(response.toString(), Version[].class);
if (ver[0].getVersion().equals(version)){
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
finish();
}else{
download(ver[0].getLink());
}
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Version ver=new Gson().fromJson(response.toString(), Version.class);
if (ver.getVersion().equals(version)){
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
finish();
}else{
download(ver.getLink());
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Version[] ver=new Gson().fromJson(errorResponse.toString(), Version[].class);
if (ver[0].getVersion().equals(version)){
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
finish();
}else{
download(ver[0].getLink());
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Toast.makeText(SplashActivity.this, &quot;success Jobj&quot;, Toast.LENGTH_SHORT).show();
Version ver=new Gson().fromJson(errorResponse.toString(), Version.class);
if (ver.getVersion().equals(version)){
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
SplashActivity.this.finish();
}else{
download(ver.getLink());
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
SplashActivity.this.finish();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
super.onSuccess(statusCode, headers, responseString);
Intent a=new Intent(SplashActivity.this,MainActivity.class);
startActivity(a);
SplashActivity.this.finish();
}
});
}
public void startAnimation(Context ctx, int view, int animation, boolean show) {
final View Element = findViewById(view);
if (show) {
Element.setVisibility(View.VISIBLE);
} else {
Element.setVisibility(View.INVISIBLE);
}
Animation anim = AnimationUtils.loadAnimation(ctx, animation);
Element.startAnimation(anim);
}
}

Volley error in Crashlytics

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method &#39;boolean java.lang.String.equals(java.lang.Object)&#39; on a null object reference
com.orangevpninfo.orangevpn.SplashActivity$3.onResponse (SplashActivity.java:17)
com.android.volley.toolbox.JsonRequest.deliverResponse (JsonRequest.java:17)
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run (ExecutorDelivery.java:2)
android.os.Handler.handleCallback (Handler.java:751)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6138)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:893)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:783)

AsyncHttpClient error Crashlytics

Fatal Exception: java.lang.RuntimeException
java.lang.NullPointerException: Attempt to invoke virtual method &#39;java.lang.String org.json.JSONObject.toString()&#39; on a null object reference
com.loopj.android.http.AsyncHttpResponseHandler.onUserException (AsyncHttpResponseHandler.java:28)
com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage (AsyncHttpResponseHandler.java)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6138)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:893)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:783)
Caused by java.lang.NullPointerException
Attempt to invoke virtual method &#39;java.lang.String org.json.JSONObject.toString()&#39; on a null object reference
com.orangevpninfo.orangevpn.SplashActivity$4.onFailure (SplashActivity.java)
com.loopj.android.http.JsonHttpResponseHandler.onFailure (JsonHttpResponseHandler.java:19)
com.loopj.android.http.AsyncHttpResponseHandler.handleMessage (AsyncHttpResponseHandler.java:19)
com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage (AsyncHttpResponseHandler.java)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6138)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:893)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:783)

答案1

得分: 2

看起来你得到的响应是空的,或者ver.getVersion()可能也会返回空。因为在这个语句中,ver.getVersion().equals(version)出现了空指针异常。

你可以尝试两件可能的事情:

  1. 你是否在发布时启用了ProGuard?如果是的话,在Version模型类中添加规则或添加@Keep注释。

  2. 如果第一步已经检查过了,你需要检查从服务器得到的响应是否有效。检查有效响应是一个很好的实践。只需添加if (response == null),并使用日志调试你的代码。

英文:

It looks like the response you are getting is null or ver.getVersion() may also return null. As there is a null pointer exception on ver.getVersion().equals(version) this statement.

Two possible things you can try-

  1. Did you turn on ProGaurd on release? If yes, add rules or add @Keep anotation in Version model class.

  2. If the first step is checked, you have to check the response from server is valid or not. It is always a good practice to check for valid response. Simply add if (response == null) and debug your code using log.

huangapple
  • 本文由 发表于 2020年10月5日 16:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64205097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定