Мои данные не анализируются в базе данных. Кроме того, название моего города и страны не выбирается из местоположения пользователя. - PullRequest
1 голос
/ 29 сентября 2019

Я написал код для регистрации пользователя и входа в систему.Я перепробовал много способов анализа данных с использованием JSON volley для базы данных phpmysql.Но я не почему не разбираюсь.Даже тост сообщение также не появляется, когда я вошел в зарегистрированную кнопку.В этом коде я также должен получить название местоположения пользователя и название страны и страны, но на практике он работал нормально, когда я применил этот метод в этом коде, он тоже не работает .. Пожалуйста, помогите мне, как это исправить ..?

Я перепробовал много методов.Но ничего не происходит ..

ОБСЛУЖИВАНИЕ

public class Register extends AppCompatActivity implements LocationListener {


    private TextView City;

    private TextView Country;

    private LocationManager locationManager;

    private double longtitude;

    private double latitude;

    Button signed_up;

    EditText userName, Email, Password, Phone_Number;

    String name, email, password, phone, city, country;

    AlertDialog.Builder builder;

    String reg_url = APIConfig.Signup;

    Context context;
//    ProgressDialog progressDialog = new ProgressDialog(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.donor_sign_up);
        context = this;

        signed_up = (Button)findViewById(R.id.sign_up2);

        userName = (EditText)findViewById(R.id.username1);

        Email  = (EditText)findViewById(R.id.emailAddress2);

        Password  = (EditText)findViewById(R.id.password2);

        Phone_Number  = (EditText)findViewById(R.id.phone2);

        City = (TextView) findViewById(R.id.city1);

        Country = (TextView) findViewById(R.id.country1);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
            return;
        }

        Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

        onLocationChanged(location);

        loc_func(location);

        signed_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name = userName.getText().toString().trim();

                email = Email.getText().toString().trim();

                password = Password.getText().toString().trim();

                phone = Phone_Number.getText().toString().trim();

                city = City.getText().toString().trim();

                country = Country.getText().toString().trim();

                if(name.equals("") || email.equals("") || password.equals("") || phone.equals("") ){


                    Toast.makeText(context,
                            "Please enter your details!", Toast.LENGTH_LONG)
                            .show();
                  //  displayAlert("input error");
                }
                else{

                    StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {

                                    try {
                                        JSONArray jsonArray = new JSONArray(response);

                                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                                        String code = jsonObject.getString("TRUE");
                                        String msg = jsonObject.getString("User already exist with that email");

                                        if(code.equals("FALSE")){

                                            Toast.makeText(context,"Registered Successfully",Toast.LENGTH_LONG).show();

                                            Intent intent = new Intent(context,Login.class);
                                            startActivity(intent);
                                           finish();
                                        }else{
                                            if(msg.equals("User already exist with that email")){

                                                Toast.makeText(context,"User already exist",Toast.LENGTH_LONG).show();
                                                Email.setText("");
                                                Password.setText("");
                                            }
                                            else if(msg.equals("Error white inserting")){

                                                Toast.makeText(context,"Registration Failed..",Toast.LENGTH_LONG).show();
                                                userName.setText("");
                                                Email.setText("");
                                                Password.setText("");
                                                Phone_Number.setText("");
                                            }

                                        }
                                       // String message = jsonObject.getString("message");

                                     Response..");

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }){

                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {

                            Map<String,String> params = new HashMap<String,String>();

                            params.put("name",name);
                            params.put("email",email);
                            params.put("password",password);
                            params.put("phoneNo",phone);
                            params.put("city",city);
                            params.put("country",country);
                            return params;
                        }
                    };

                    MySingleton.getInstance(Register.this).addToRequestQueue(stringRequest);
                }
            }
        });
    }



    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longtitude = location.getLongitude();
    }


    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    private void loc_func(Location location){

        try {
            Geocoder geocoder = new Geocoder(this);
            List<Address> addresses = null;
            addresses = geocoder.getFromLocation(latitude,longtitude,1);
            String country = addresses.get(0).getCountryName();
            String city = addresses.get(0).getLocality();

            City.setText(city);
            Country.setText(country);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this,"Error"+e,Toast.LENGTH_SHORT).show();
        }

    }
}

ФАЙЛ XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootlayout"
    tools:ignore="InvalidId">

   <!-- <ImageView
        android:layout_width="86dp"
        android:layout_height="84dp"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/help"></ImageView>-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register New User"
        android:textColor="@color/image_color"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="120dp"></TextView>

    <TextView
        android:id="@+id/textView9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="User Name :"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="50dp"/>

    <EditText
        android:id="@+id/username1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:inputType="textPersonName"
        />

    <TextView
        android:id="@+id/email2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Email Address :"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"/>

    <EditText
        android:id="@+id/emailAddress2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/paswrd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password :"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"
        />

    <EditText
        android:id="@+id/password2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:layout_marginLeft="60dp"
        />

    <TextView
        android:id="@+id/textView14"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Phone Number :"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"/>

    <EditText
        android:id="@+id/phone2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:inputType="phone" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/city1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Getting City.."
            android:textColor="#000000"
            android:textStyle="bold"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="10dp"/>

        <TextView
            android:id="@+id/country1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Getting Country.."
            android:textColor="#000000"
            android:textStyle="bold"
            android:layout_marginLeft="125dp"
            android:layout_marginTop="10dp"></TextView>
    </LinearLayout>
    <Button
        android:id="@+id/sign_up2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CREATE NEW ACCOUNT"
        android:background="@drawable/button_circular"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="60dp"
        android:layout_marginTop="30dp"
        android:textColor="#ffffff"
        android:textSize="16sp"
        android:textStyle="bold"/>



</LinearLayout>

ФАЙЛ PHP

<?php

require_once '../include/DB_functions.php';

$db = new DB_functions();

//echo getcwd();

if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['city']) && isset($_POST['country']) && isset($_POST['email']) && isset($_POST['phoneNo'])) {
    $name = $_POST['name'];
    $password = $_POST['password'];
    $email = $_POST['email'];
   $phoneNo = $_POST['phoneNo']; 
      $city = $_POST['city'];    
   $country = $_POST['country'];

    $result = $db->userAlreadyExist($email);
    if($result){
        $response['error'] = TRUE;
        $response['error_msg'] = "User already exist with that email";
        echo json_encode($response);

    }
    else{

    $result = $db->insertUser($name, $email, $password, $phoneNo, $city, $country);
    if ($result) {
        $response['error'] = FALSE;
        $response['error_msg'] = "Successful";
        echo json_encode($response);
    } else {

        $response['error'] = TRUE;
        $response['error_msg'] = "Error white inserting";
        echo json_encode($response);
    }
    }
} else {
    $response['error'] = TRUE;
    $response['error_msg'] = "Fileds are missing";
    echo json_encode($response);
}

Ожидается:

  1. Синтаксический анализ данных.
  2. Выбор пользователя, города и страны.
  3. Отображение тостового сообщения.

Фактический результат:

Ничего не происходит.

1 Ответ

0 голосов
/ 29 сентября 2019

Вы вставляете данные сюда, а не извлекаете.

...