如何使用json android向服务器发送单选按钮值和切换案例选项?
我正在尝试制作一个注册表,其中所有值都保存在服务器上,但我不知道如何将单选按钮的值和切换选项按钮的值发送到服务器。 我尝试为单选按钮实现此功能,但当此活动打开时,它也只显示单选按钮值一次。请告诉我单选按钮和开关的正确操作方法 登记活动如何使用json android向服务器发送单选按钮值和切换案例选项?,android,json,switch-statement,radio-group,Android,Json,Switch Statement,Radio Group,我正在尝试制作一个注册表,其中所有值都保存在服务器上,但我不知道如何将单选按钮的值和切换选项按钮的值发送到服务器。 我尝试为单选按钮实现此功能,但当此活动打开时,它也只显示单选按钮值一次。请告诉我单选按钮和开关的正确操作方法 登记活动 package com.example.zeba.brocoli; 导入android.app.ProgressDialog; 导入android.content.Context; 导入android.content.Intent; 导入android.os.As
package com.example.zeba.brocoli;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.Switch;
导入android.widget.RadioGroup.OnCheckedChangeListener;
导入android.widget.Toast;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.List;
公共类注册表单扩展了AppCompativeActivity{
编辑文本fn、ln、mb、em、pw、cpw、dob、gen;
开关开关;
放射组男性,女性;
开关swth;
私人对话;
私有静态字符串url_create_book=”http://cloud.....com/broccoli/creatinfo.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
JSONParser JSONParser=新的JSONParser();
私有int-serverResponseCode=0;
上下文c;
int i=0;
按钮接头;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u注册表);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
fn=(EditText)findViewById(R.id.fnm);
ln=(EditText)findViewById(R.id.lnm);
mb=(EditText)findviewbyd(R.id.mobile);
em=(EditText)findViewById(R.id.email);
pw=(EditText)findViewById(R.id.pass);
cpw=(EditText)findViewById(R.id.cpass);
RadioButton男性=(RadioButton)findViewById(R.id.rgm);
RadioButton feml=(RadioButton)findViewById(R.id.rgf);
开关swth=(开关)findviewbyd(R.id.mySwitch);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
sub=(按钮)findViewById(R.id.sub2);
addListenerOnButton();
sub.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建CreateNewProduct().execute();
//startActivity(新意图(RegistrationForm.this,Home.class));
}
});
}
public void addListenerOnButton(){
放射组rgrp=(放射组)findViewById(R.id.rg);
单选按钮单选按钮;
//从radioGroup中获取所选单选按钮
int selectedId=rgrp.getCheckedRadioButtonId();
//通过返回的id查找radiobutton
radioButton=(radioButton)findViewById(selectedId);
Toast.makeText(RegistrationForm.this,
radioButton.getText(),Toast.LENGTH_SHORT.show();
}
类CreateNewProduct扩展了AsyncTask{
私有字符串fname;
私有字符串名称;
私人字符串电子邮件;
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(RegistrationForm.this);
pDialog.setMessage(“创建书籍…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
fname=fn.getText().toString();
lname=ln.getText().toString();
email=em.getText().toString();
}
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“名字”,fname));
添加参数(新的BasicNameValuePair(“姓氏”,lname));
参数添加(新的BasicNameValuePair(“电子邮件”),电子邮件);
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u create\u book,
“POST”,params);
//检查cat fro响应日志
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建产品
Intent i=新的Intent(getApplicationContext(),Login.class);
星触觉(i);
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
}
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
onBackPressed();
返回true;
违约:
package com.example.zeba.broccoli;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class RegistrationForm extends AppCompatActivity {
EditText fn,ln,mb,em,pw,cpw,dob,gen;
Switch sw;
RadioGroup male,feml;
Switch swth;
private ProgressDialog pDialog;
private static String url_create_book = "http://cloud.....com/broccoli/creatinfo.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
JSONParser jsonParser = new JSONParser();
private int serverResponseCode = 0;
Context c;
int i=0;
Button sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration_form);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fn=(EditText)findViewById(R.id.fnm) ;
ln=(EditText)findViewById(R.id.lnm) ;
mb=(EditText)findViewById(R.id.mobile) ;
em=(EditText)findViewById(R.id.email) ;
pw=(EditText)findViewById(R.id.pass) ;
cpw=(EditText)findViewById(R.id.cpass) ;
RadioButton male=(RadioButton)findViewById(R.id.rgm) ;
RadioButton feml=(RadioButton)findViewById(R.id.rgf) ;
Switch swth=(Switch)findViewById(R.id.mySwitch) ;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
sub=(Button)findViewById(R.id.sub2);
addListenerOnButton();
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CreateNewProduct().execute();
// startActivity(new Intent(RegistrationForm.this, Home.class));
}
});
}
public void addListenerOnButton() {
RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);
RadioButton radioButton;
// get selected radio button from radioGroup
int selectedId = rgrp.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(RegistrationForm.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
class CreateNewProduct extends AsyncTask<String, String, String> {
private String fname;
private String lname;
private String email;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegistrationForm.this);
pDialog.setMessage("Creating books..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
fname = fn.getText().toString();
lname = ln.getText().toString();
email = em.getText().toString();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("First_Name", fname));
params.add(new BasicNameValuePair("Last_Name",lname));
params.add(new BasicNameValuePair("email", email));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_book,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zeba.broccoli.Login">
<!-- Registration Form -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dip"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<!-- Full Name Label -->
<EditText android:id="@+id/fnm"
android:hint="First Name"
android:layout_width="fill_parent"
android:background="@drawable/rounded_edittext"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dip"/>
<EditText android:id="@+id/lnm"
android:hint="Last Name"
android:layout_width="fill_parent"
android:background="@drawable/rounded_edittext"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:layout_marginBottom="20dip"/>
<EditText android:id="@+id/mobile"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:background="@drawable/rounded_edittext"
android:hint="Mobile Number"
android:layout_marginBottom="20dip"/>
<!-- Password Label -->
<EditText android:id="@+id/email"
android:layout_width="fill_parent"
android:hint="Email"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:background="@drawable/rounded_edittext"
android:layout_marginBottom="20dip"/>
<EditText android:id="@+id/pass"
android:layout_width="fill_parent"
android:hint="password"
android:inputType="textPassword"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:background="@drawable/rounded_edittext"
android:layout_marginBottom="20dip"/>
<!-- Register Button -->
<EditText android:id="@+id/cpass"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:inputType="textPassword"
android:background="@drawable/rounded_edittext"
android:hint="Confirm password"
android:layout_marginBottom="20dip"/>
<EditText android:id="@+id/dob"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:background="@drawable/rounded_edittext"
android:hint="Date of Birth(Optional)"
android:layout_marginBottom="20dip"/>
<!-- Register Button -->
<!-- Link to Login Screen -->
<TextView
android:id="@+id/gen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:text="Gender"
android:textSize="18dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<RadioGroup
android:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rgm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:checked="true"
android:layout_weight="1"
android:textSize="14dp"
android:text="Male"
/>
<RadioButton
android:id="@+id/rgf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:checked="false"
android:layout_weight="1"
android:textSize="14dp"
android:text="Female"
/>
</RadioGroup>
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"/>
<TextView android:id="@+id/rupdates"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:layout_marginBottom="20dip"
android:text="Receive updates on offers, promotions and discounts"
android:gravity="center"
android:textSize="20dip"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="5dp">
<TextView android:id="@+id/sms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:text="Subscribe to sms"
android:textSize="20dip"
/>
<Switch
android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/sub2"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Submit"
android:background="@drawable/mybutton"
android:textColor="#fff"
android:textStyle="bold"
android:elevation="0dp" />
</RelativeLayout>
</LinearLayout>
<!-- Registration Form Ends -->
</ScrollView>
rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
}
});
radio_button_value1 = ((RadioButton) findViewById(radioGroup1.getCheckedRadioButtonId())).getText().toString();
// get selected radio button from radioGroup
int selectedId = rgrp.getCheckedRadioButtonId();
// get selected radio button from radioGroup
int selectedId = rgrp.getCheckedRadioButtonId().getText().toString();
public void onClick(View v) {
RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);
RadioButton radioButton;
int selectedId = rgrp.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(RegistrationForm.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
String gender="Male" ;
boolean switchValue = false ;
public void addListenerOnButton() {
RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);
rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==R.id.rgm){
gender="Male" ;
}else{
gender="Female";
}
}
});
Toast.makeText(RegistrationForm.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
}
switchValue = swth.isChecked();