1

I am trying to get and send data by using Volley networking library. When I first Install my app the data is coming same which is in server and when I update it, it's running successfully but when I update it again the data is not going to the server and showing previous data. I checked lot's off tutorial or search about it but not find the result. So please can anyone help me to solve this error.

Below Class is for to get the data from server

public class ManageWebsiteActivity extends AppCompatActivity {
// Session Manager Class
UserSessionManager session;
String user_id, str_address, str_city, str_state, str_country, str_pincode;

TextView editBasicInfo, BusinessName, BusinessCategory;
private Toolbar toolbar;
TableRow businessAddress,contactInformation, businessHours, businessLogo, siteAppearance, photoGallery, customPage;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext() ,R.color.colorPrimary));
    setContentView(R.layout.activity_manage_website);

    //session manager
    session = new UserSessionManager(getApplicationContext());
    session.checkLogin();
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    user_id = user.get(UserSessionManager.KEY_USER_ID);

    editBasicInfo= (TextView)findViewById(R.id.textView3);
    BusinessName= (TextView)findViewById(R.id.textView);
    BusinessCategory= (TextView)findViewById(R.id.textView2);
    businessAddress= (TableRow)findViewById(R.id.businessAddress);
    contactInformation=(TableRow)findViewById(R.id.contactInformation);
    businessHours= (TableRow)findViewById(R.id.businessHour);
    businessLogo= (TableRow)findViewById(R.id.businessLogo);
    siteAppearance= (TableRow)findViewById(R.id.siteAppearance);
    photoGallery= (TableRow)findViewById(R.id.photoGallery);
    customPage= (TableRow)findViewById(R.id.customPage);

    getData();

    businessAddress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent editIntent = new Intent(ManageWebsiteActivity.this,BusinessAddress.class);
            editIntent.putExtra("str_address",str_address);
            Log.e("add________",str_address);
            editIntent.putExtra("str_city",str_city);
            editIntent.putExtra("str_state",str_state);
            editIntent.putExtra("str_country",str_country);
            editIntent.putExtra("str_pincode",str_pincode);
            startActivity(editIntent);
        }
    });
  }
  private void getData() {
    String getBusinessAddressURL ="XYZ.php?username="+user_id;
    StringRequest stringRequest = new StringRequest(Request.Method.POST,getBusinessAddressURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ManageWebsiteActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(ManageWebsiteActivity.this);
    requestQueue.add(stringRequest);
}

public void showJSON(String response){
    String str_address="";
    String str_city="";
    String str_state = "";
    String str_country = "";
    String str_pincode = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        str_address = jsonObject.getString(Config.KEY_ADDRESS);
        this.str_address=str_address;

        str_city = jsonObject.getString(Config.KEY_CITY);
        this.str_city=str_city;

        str_state = jsonObject.getString(Config.KEY_STATE);
        this.str_state=str_state;

        str_country = jsonObject.getString(Config.KEY_COUNTRY);
        this.str_country=str_country;

        str_pincode = jsonObject.getString(Config.KEY_PINCODE);
        this.str_pincode=str_pincode;

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

Below Class is for to Update the data from server

public class BusinessAddress extends AppCompatActivity {

public static final String KEY_ADDRESS = "address";
public static final String KEY_CITY = "city";
public static final String KEY_STATE = "state";
public static final String KEY_COUNTRY = "country";
public static final String KEY_PIN = "pincode";

private EditText etAddress;
private EditText etCity;
private EditText etPin;
private EditText etState;
private EditText etCountry;
private Button buttonBusinessAddress;

private String user_id, str_address, str_city, str_state, str_country, str_pincode;

// Session Manager Class
UserSessionManager session;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_business_address);
    getSupportActionBar().setTitle("Business Address");

    //session manager
    session = new UserSessionManager(getApplicationContext());
    session.checkLogin();
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();

    user_id = user.get(UserSessionManager.KEY_USER_ID);
    etAddress = (EditText)findViewById(R.id.etAddress) ;
    etCity = (EditText)findViewById(R.id.etCity) ;
    etPin = (EditText)findViewById(R.id.etPin) ;
    etState = (EditText)findViewById(R.id.etState) ;
    etCountry = (EditText)findViewById(R.id.etCountry) ;
    buttonBusinessAddress = (Button)findViewById(R.id.buttonBusinessAddress);

    Bundle b = getIntent().getExtras();
    if (b!=null){
        str_address = b.getString("str_address");
        str_city = b.getString("str_city");
        str_state = b.getString("str_state");
        str_country = b.getString("str_country");
        str_pincode = b.getString("str_pincode");
    }

    etAddress.setText(str_address);
    etCity.setText(str_city);
    etState.setText(str_state);
    etCountry.setText(str_country);
    etPin.setText(str_pincode);


    buttonBusinessAddress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendData();
        }
    });
}

private void sendData() {
    String UPDATE_URL = "XYZ.php?username="+user_id;
    Log.e("URL_________________",UPDATE_URL);

    final String str_address = etAddress.getText().toString().trim();
    final String str_city = etCity.getText().toString().trim();
    final String str_state = etState.getText().toString().trim();
    final String str_country = etCountry.getText().toString().trim();
    final String str_pin = etPin.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPDATE_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if(response.equalsIgnoreCase("success")){
                Log.e("send response________",response) ;
                Toast.makeText(BusinessAddress.this, "Business Address has been updated", Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(BusinessAddress.this, "Something Wrong" , Toast.LENGTH_LONG).show();
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(BusinessAddress.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put(KEY_ADDRESS, str_address);
            params.put(KEY_CITY, str_city);
            params.put(KEY_STATE, str_state);
            params.put(KEY_COUNTRY, str_country);
            params.put(KEY_PIN, str_pin);
            return params;
        }

    };
    RequestQueue requestQueue = Volley.newRequestQueue(BusinessAddress.this);
    requestQueue.add(stringRequest);
  }
}
Anatolii
  • 14,139
  • 4
  • 35
  • 65
Paramjeet Singh
  • 145
  • 1
  • 3
  • 15
  • Possible duplicate of [How do you use the Android Volley API?](https://stackoverflow.com/questions/17571759/how-do-you-use-the-android-volley-api) – InsaneCat Jun 30 '18 at 05:49
  • Thanks for reply, but sir I have already used volley library in my previous project but in this project I faced this issue first time. – Paramjeet Singh Jun 30 '18 at 05:54

1 Answers1

2

There is a problem in Cache so I used requestQueue.getCache().clear(); and solved it.

Paramjeet Singh
  • 145
  • 1
  • 3
  • 15