gpt4 book ai didi

php - 将数组传递给 BasicNameValuePair?

转载 作者:行者123 更新时间:2023-11-29 22:08:15 25 4
gpt4 key购买 nike

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

我选择兴趣的 Activity

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 脚本中执行“interests”数组。我基本上使用 "switch ( $_POST['userInterests'] ) {......."

最佳答案

你设置了onCheckedChangedListener还是OnClickListener吗?

关于php - 将数组传递给 BasicNameValuePair?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com