在android中使用Volley Library时数据未更新

时间:2018-06-30 05:44:48

标签: java android android-volley

我正在尝试使用Volley网络库获取和发送数据。当我第一次安装我的应用程序时,服务器中的数据和更新时的数据相同,并且运行成功,但是当我再次对其进行更新时,数据将不会发送到服务器并显示以前的数据。我检查了很多教程或进行搜索,但未找到结果。因此,请任何人可以帮助我解决此错误。

下面的类用于从服务器获取数据

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();
    }
   }
  }

以下类别用于更新服务器中的数据

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);
  }
}

1 个答案:

答案 0 :(得分:1)

缓存中存在问题,因此我使用了requestQueue.getCache().clear();并解决了该问题。