- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这听起来很疯狂,但我知道“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/
我目前正在使用此代码在数据库中存储两个字符串(newCategory 和 reportDate): String newCategory = spinnerFood.getSelectedItem()
我知道这听起来很疯狂,但我知道“BasicNameValuePair”实际上已被弃用!但我的老板告诉我出于某种原因要使用它。我有多个复选框,用户需要选择他们的兴趣,这些数据被发送并保存在 mysql
我正在尝试通过 Java 中的 BasicNameValePair 发送数组。是否可能,如果可能,如何实现。 字符串[]参数 = {"19"};params.add(new BasicNameValu
我问了问题 here .现在我可以将 arraylist 传递给另一个 Activity 。 在我的第二个 Activity 中,我试图发布这个数组列表。 ArrayList testing = th
我有一个构建 HttpResponse 初始值设定项的类。在应返回 BasicNameValuePair 的方法之一中,我必须检查列表中是否存在具有由字符串“name”指定的键或名称的条目。 publ
如何使用 ArrayList 创建此 JSON 以在 Android 中发布 json { "user" : { "nickname" : "nickname6",
我正在尝试使用 Java 循环 BasicNameValuePair。我已经有一个 PHP 的工作示例。这是我所拥有的: ArrayList data = new ArrayList(); pos
我必须将参数列表传递给 Http POST 调用。 服务器期望的实际 JSON 是: { "par1": "val1", "par1": "val1", "par3": ["val1", "va
我遇到了 BasicNameValuePair 的问题。我想将 ArrayList 传递到 BasicNameValuePair 中。通过 NameValuePair,我想将此 arrayList 发
我在 ListActivity 上收到“未找到任何项目” toast 。 注意:当我将 PHP 代码更改为“Select * from items”时,会显示整个表格。但是当我尝试在 Android/
我正在尝试向本地服务器发送 http POST 请求 这是代码: public JSONObject makeHttpRequest (String url, String method, List
我正在使用一些在线教程学习 Android。除了这个之外,我所有的 Java 类(class)都很好。 @Override protected Void doInBackground(
我目前正在尝试反序列化 API 结果,如下所示 [{"name":"MyName","value":"MyValue"},{"name":"MyName2","value":"MyValue2"}]
我是 Android 应用程序的新手。要点我想将数据从 android 应用程序发送到 php 页面,但我的代码中出现以下错误 org.apache.http.impl.client.defaulth
我正在尝试为一些 BasicNameValuePair 对象列表实现自定义 gson 序列化器/反序列化器。 我在这里看到了部分解决方案代码(用于序列化): How do I get Gson to
我必须将 BasicNameValuePair 的 ArrayList 发送到我的 Android 应用程序中的另一个 Activity。首先,它不允许我这样做,说 BasicNameValuePai
我想在电子邮件正文中插入 html 代码...在我的代码中,我在后台发送电子邮件意味着按钮的 onClick 事件,因为每当我在 BasicNameValuePair 中使用 Html.formHtm
我是一名优秀的程序员,十分优秀!