将数组传递给BasicNameValuePair?

时间:2015-08-12 01:01:35

标签: php android mysql

我知道这听起来很疯狂,但我知道“BasicNameValuePair”实际上已被弃用!但我的老板告诉我出于某种原因使用它。我有多个复选框,用户需要选择他们的兴趣,这些数据被发送并保存在mysql数据库中。我的问题是一切正常,但我只获得了被点击保存在数据库中的最后一个值。尽管如此,我还尝试了"interests[]",但这也没有帮助。

我选择兴趣的活动

package com.blueflair.incre;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.CheckedOutputStream;


public class ChooseInterestActivity extends AppCompatActivity {

public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://192.168.0.13/serverfiles/";

CheckBox news, people, fashion, jobs, foodAndDrink, cooking, musicAndMovie, healthAndFitness, animalsAndPets,
celebrities, photography, events, games, artAndDesign, technology, womensFashion, mensFashion, education, information;

ProgressDialog progressDialog;
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
LocalUserData userLocalStore;
User user;
Button next_execute, backButtonInterest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_interest);

    //Instantiating array for the data to be sent
    next_execute = (Button) findViewById(R.id.next_execute);
    backButtonInterest = (Button) findViewById(R.id.backButtonInterest);
    //Instantiating the checkboxes to their respective ID's
    news = (CheckBox) findViewById(R.id.newsCb);
    people = (CheckBox) findViewById(R.id.peopleCb);
    fashion = (CheckBox) findViewById(R.id.fashionCb);
    jobs = (CheckBox) findViewById(R.id.jobsCb);
    foodAndDrink = (CheckBox) findViewById(R.id.foodanddrinkCb);
    cooking = (CheckBox) findViewById(R.id.cookingCb);
    musicAndMovie = (CheckBox) findViewById(R.id.musicAndMovieCb);
    healthAndFitness = (CheckBox) findViewById(R.id.healthAndFitnessCb);
    animalsAndPets = (CheckBox) findViewById(R.id.animalsAndPetCb);
    celebrities = (CheckBox) findViewById(R.id.celebritiesCb);
    photography = (CheckBox) findViewById(R.id.photographyCb);
    events = (CheckBox) findViewById(R.id.eventsCb);
    games = (CheckBox) findViewById(R.id.gamesCb);
    artAndDesign = (CheckBox) findViewById(R.id.artCb);
    technology = (CheckBox) findViewById(R.id.technologyCb);
    womensFashion = (CheckBox) findViewById(R.id.womensFashionCb);
    mensFashion = (CheckBox) findViewById(R.id.mensFashionCb);
    education = (CheckBox) findViewById(R.id.educationCb);
    information = (CheckBox) findViewById(R.id.informationCb);

    userLocalStore= new LocalUserData(this);
    storeUserCheckBox();

    //For executing the next button when clicked
    next_execute.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            user = userLocalStore.getLoggedInUser();
            storeUserInterest(user);
        }
    });

    //For going back to the last activity when clicked
    backButtonInterest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}
//Store user checkbox method to be stored in dataToSend array
private void storeUserCheckBox() {
    if(news.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "news"));
    }
    if(people.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "people"));
    }
    if(fashion.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "fashion"));
    }
    if(jobs.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "jobs"));
    }
    if(foodAndDrink.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "foodAndDrink"));
    }
    if(cooking.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "cooking"));
    }
    if(musicAndMovie.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "musicAndMovie"));
    }
    if(healthAndFitness.isChecked()){
        dataToSend.add(new BasicNameValuePair("huserInterests", "healthAndFitness"));
    }
    if(animalsAndPets.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "animalsAndPets"));
    }
    if(celebrities.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "celebrities"));
    }
    if(photography.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "photography"));
    }
    if(events.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "events"));
    }
    if(games.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "games"));
    }
    if(artAndDesign.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "artAndDesign"));
    }
    if(technology.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "technology"));
    }
    if(womensFashion.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "womensFashion"));
    }
    if(mensFashion.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "mensFashion"));
    }
    if(education.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "education"));
    }
    if(information.isChecked()){
        dataToSend.add(new BasicNameValuePair("userInterests", "information"));
    }
}

//Executing the AsyncTask
public void storeUserInterestInBackground(User user, GetUserCallback userCallBack) {
    progressDialog.show();
    new StoreUserInterestAsyncTask(user, userCallBack).execute();
}


private void setProgressDialog(Context context) {
    progressDialog = new ProgressDialog(context);
    progressDialog.setCancelable(false);
    progressDialog.setTitle("Processing...");
    progressDialog.setMessage("Please wait...");
}

//AsyncTask that controlls the behavior in the background

class StoreUserInterestAsyncTask extends AsyncTask<Void, Void, Void> {
    User user;
    GetUserCallback userCallBack;

    public StoreUserInterestAsyncTask(User user, GetUserCallback userCallBack) {
        this.user = user;
        this.userCallBack = userCallBack;
    }

    @Override
    protected Void doInBackground(Void... params) {
        user = userLocalStore.getLoggedInUser();
        dataToSend.add(new BasicNameValuePair("userID", user.userID + ""));

        //Method to store the click checkbox to the "Data to Send" array
        storeUserCheckBox();

        HttpParams httpRequestParams = getHttpRequestParams();

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS
                + "interestPage.php");

        try {
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private HttpParams getHttpRequestParams() {
        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        return httpRequestParams;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
        userCallBack.done(null);
    }

}
//UserCallback for when everything is done
private void storeUserInterest(User user) {
    setProgressDialog(this);
    storeUserInterestInBackground(user, new GetUserCallback() {
        @Override
        public void done(User returnedUser) {
            Intent loginIntent = new Intent(ChooseInterestActivity.this, MainActivity.class);
            startActivity(loginIntent);
        }
    });
}

}

我已经从我的php“interestPage.php”文件成功建立了与数据库的连接。以及如何在PHP脚本中执行“兴趣”数组。 我基本上使用"switch ( $_POST['userInterests'] ) {......."

2 个答案:

答案 0 :(得分:0)

您是否设置了onCheckedChangedListener或OnClickListener?

答案 1 :(得分:0)

我认为,因为您在storeUserCheckBox中致电onCreate并且每次都向服务器发送数据 - &gt;如果用户点击一次复选框,他们的共同作用NameValuePair将一直退出。

我认为你应该在发送到服务器之前清除dataToSend

user = userLocalStore.getLoggedInUser();
dataToSend.clear();
dataToSend.add(new BasicNameValuePair("userID", user.userID + ""));

//Method to store the click checkbox to the "Data to Send" array
storeUserCheckBox();