简体   繁体   English

在移动数据连接截击上运行的 Android 应用程序

[英]Android app run on mobile data connection volley

How to run Android app on mobile data with volley or asynctask or Advance networking library?如何使用 volley 或 asynctask 或 Advance 网络库在移动数据上运行 Android 应用程序? I have tried a lot of solution but my Android app not run on mobile data.我尝试了很多解决方案,但我的 Android 应用程序无法在移动数据上运行。 I have increased volley timeout but not resolve my problem.我增加了截击超时但没有解决我的问题。

Below show my all code with volley library but my problem are not solved with this code also.下面显示了我使用 volley 库的所有代码,但我的问题也没有用这段代码解决。

firstName = etFirstName.getText().toString();
        lastName = etLastName.getText().toString();
        email = etEmail.getText().toString();
        password = etPassword.getText().toString();
        if (TextUtils.isEmpty(firstName)) {
            etFirstName.setError("Required");
            etFirstName.requestFocus();
        } else if (TextUtils.isEmpty(lastName)) {
            etLastName.setError("Required");
            etLastName.requestFocus();
        } else if (TextUtils.isEmpty(email)) {
            etEmail.setError("Required");
            etEmail.requestFocus();
        } else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            etEmail.setError("invalid email");
            etEmail.requestFocus();
        } else if (TextUtils.isEmpty(password)) {
            etPassword.setError("Required");
            etPassword.requestFocus();
        } else {
            etFirstName.setError(null);
            etLastName.setError(null);
            etEmail.setError(null);
            etPassword.setError(null);
            if (isNetworkAvailable()) {
                RequestQueue queue = Volley.newRequestQueue(getActivity());
                try {
                    dialog.show();

                    // Request a string response from the provided URL.
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.url + "register.php",
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String result) {
                                    if (!dialog.isCancelled()) {
                                        dialog.dismiss();
                                    }
                                    if (result.equals("email already exist!")) {
                                        Toast.makeText(getActivity(), "Entered email already exist!", Toast.LENGTH_SHORT).show();
                                    } else if (result.equals("Error")) {
                                        Toast.makeText(getActivity(), "Insert Record Error!", Toast.LENGTH_SHORT).show();
                                    } else {
                                        try {
                                            JSONObject response = new JSONObject(result);
                                            String userEmail = response.getString("email");
                                            if (userEmail != null && !userEmail.isEmpty()) {
                                                for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
                                                    fragmentManager.popBackStack();
                                                }
                                                String userID = response.getString("id");
                                                String fbID = response.getString("fb_id");
                                                String googleID = response.getString("google_id");
                                                String firstName = response.getString("first_name");
                                                String lastName = response.getString("last_name");
                                                String userPass = response.getString("password");
                                                String userMobile = response.getString("mobile");
                                                String userLocation = response.getString("location");
                                                String userDOB = response.getString("dob");
                                                String userGender = response.getString("gender");
                                                String posts = response.getString("posts");
                                                String following = response.getString("following");
                                                String followers = response.getString("followers");
                                                String userImagePath = response.getString("image_path");
                                                sessionManager.createLoginSession(userID, userEmail, fbID, googleID,
                                                        firstName, lastName, userPass, userMobile, userLocation,
                                                        userDOB, userGender, userImagePath, posts, following, followers);
                                                //fragmentManager.beginTransaction().replace(R.id.framePreLogin, new EnterPhoneFragment()).commit();
                                                saveSetting(userID);
                                            } else {
                                                Toast.makeText(getActivity().getApplicationContext(),
                                                        "Profile creation failed. Try again!",
                                                        Toast.LENGTH_SHORT).show();
                                            }

                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            if (!dialog.isCancelled()) {
                                dialog.dismiss();
                            }
                            if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                                Toast.makeText(getActivity(), "Network Connection Timeout!", Toast.LENGTH_LONG).show();
                            } else if (error instanceof AuthFailureError) {
                                Toast.makeText(getActivity(), "Auth failure error!", Toast.LENGTH_LONG).show();
                            } else if (error instanceof ServerError) {
                                Toast.makeText(getActivity(), "Server error!", Toast.LENGTH_LONG).show();
                            } else if (error instanceof NetworkError) {
                                Toast.makeText(getActivity(), "network error!", Toast.LENGTH_LONG).show();
                            } else if (error instanceof ParseError) {
                                Toast.makeText(getActivity(), "Parsing error!", Toast.LENGTH_LONG).show();
                            }
                        }

                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("first_name", firstName);
                            params.put("last_name", lastName);
                            params.put("email", email);
                            params.put("password", password);
                            String firebaseRegistrationId = sessionManager.getFirebaseToken();
                            params.put("firebase_registration_id", firebaseRegistrationId);
                            return params;
                        }

                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("Content-Type", "application/x-www-form-urlencoded");
                            return params;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                            MY_SOCKET_TIMEOUT_MS,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    // Add the request to the RequestQueue.
                    queue.add(stringRequest);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                Toast.makeText(getActivity().getApplicationContext(), "Network not available. Try again!", Toast.LENGTH_SHORT).show();
            }
        }

The sent data length is restricted in some devices when using mobile data, so try to make your data as less as possible.使用移动数据时,某些设备会限制发送的数据长度,因此请尽量减少数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM