Php 单选按钮不断发送错误的值
我是Android新手,最近有一个家庭作业:其中一个活动将从单选按钮和一些文本视图向数据库发送值,但当我尝试运行它并检查数据库时,它总是显示错误的值。发送正确值的唯一单选按钮是第一个 这是一项活动:Php 单选按钮不断发送错误的值,php,android,Php,Android,我是Android新手,最近有一个家庭作业:其中一个活动将从单选按钮和一些文本视图向数据库发送值,但当我尝试运行它并检查数据库时,它总是显示错误的值。发送正确值的唯一单选按钮是第一个 这是一项活动: @SuppressLint("NewApi") public class Test extends Activity { ProgressDialog pDialog; TextView idm, nama_mk, kls, mhw_mk; Button submit;
@SuppressLint("NewApi")
public class Test extends Activity {
ProgressDialog pDialog;
TextView idm, nama_mk, kls, mhw_mk;
Button submit;
String P1 = "1";
String P2 = "1";
String P3 = "1";
String P4 = "1";
String P5 = "1";
RadioGroup cho1;
RadioGroup cho2;
RadioGroup cho3;
RadioGroup cho4;
RadioGroup cho5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
cho1 = (RadioGroup) findViewById(R.id.choice1);
cho1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.A1:
P1 = "1";
break;
case R.id.B1:
P1 = "2";
break;
case R.id.C1:
P1 = "3";
break;
case R.id.D1:
P1 = "4";
break;
}
}
});
cho2 = (RadioGroup) findViewById(R.id.choice2);
cho2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.A2:
P2 = "1";
break;
case R.id.B2:
P2 = "2";
break;
case R.id.C2:
P2 = "3";
break;
case R.id.D2:
P2 = "4";
break;
}
}
});
cho3 = (RadioGroup) findViewById(R.id.choice3);
cho3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.A3:
P3 = "1";
break;
case R.id.B3:
P3 = "2";
break;
case R.id.C3:
P3 = "3";
break;
case R.id.D3:
P3 = "4";
break;
}
}
});
cho4 = (RadioGroup) findViewById(R.id.choice4);
cho4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.A4:
P2 = "1";
break;
case R.id.B4:
P2 = "2";
break;
case R.id.C4:
P2 = "3";
break;
case R.id.D4:
P2 = "4";
break;
}
}
});
cho5 = (RadioGroup) findViewById(R.id.choice5);
cho5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.A5:
P3 = "1";
break;
case R.id.B5:
P3 = "2";
break;
case R.id.C5:
P3 = "3";
break;
case R.id.D5:
P3 = "4";
break;
}
}
});
idm = (TextView) findViewById(R.id.nim_mhs);
nama_mk = (TextView) findViewById(R.id.nama_mk);
kls = (TextView) findViewById(R.id.kelas);
mhw_mk = (TextView) findViewById(R.id.id_mhw_mk);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new EntryData().execute();
}
});
}
public class EntryData extends AsyncTask<String, String, String> {
String success;
String url_link = "http://wwwww/xxxxx/yyyyy/zzzzz.php";
@Override
protected String doInBackground(String... params) {
String strnama = nama_mk.getText().toString();
String stremail = idm.getText().toString();
String strpassword = kls.getText().toString();
String strmhwmk = mhw_mk.getText().toString();
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("matakuliah", strnama));
nvp.add(new BasicNameValuePair("idmahasiswa", stremail));
nvp.add(new BasicNameValuePair("kelas", strpassword));
nvp.add(new BasicNameValuePair("id_mhw_mk", strmhwmk));
nvp.add(new BasicNameValuePair("p1", P1));
nvp.add(new BasicNameValuePair("p2", P2));
nvp.add(new BasicNameValuePair("p3", P3));
nvp.add(new BasicNameValuePair("p4", P4));
nvp.add(new BasicNameValuePair("p5", P5));
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequest(url_link, "POST", nvp);
try {
success = json.getString("success");
if (success == "1") {
Intent forum = new Intent(getApplicationContext(), ForumActivity.class);
forum.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(forum);
finish();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_LONG).show();
}
return null;
}
}
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
@SuppressLint(“NewApi”)
公共类测试扩展了活动{
ProgressDialog;
TextView idm,纳马角,吉隆坡,mhw角;
按钮提交;
字符串P1=“1”;
字符串P2=“1”;
字符串P3=“1”;
字符串P4=“1”;
字符串P5=“1”;
放射组cho1;
放射组cho2;
放射组cho3;
放射组cho4;
放射组cho5;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}
cho1=(放射组)findViewById(R.id.choice1);
cho1.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(RadioGroup组,int checkedId){
//TODO自动生成的方法存根
开关(检查EDID){
案例R.id.A1:
P1=“1”;
打破
案例R.id.B1:
P1=“2”;
打破
案例R.id.C1:
P1=“3”;
打破
案例R.id.D1:
P1=“4”;
打破
}
}
});
cho2=(放射组)findViewById(R.id.choice2);
cho2.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(RadioGroup组,int checkedId){
//TODO自动生成的方法存根
开关(检查EDID){
案例R.id.A2:
P2=“1”;
打破
案例R.id.B2:
P2=“2”;
打破
案例R.id.C2:
P2=“3”;
打破
案例R.id.D2:
P2=“4”;
打破
}
}
});
cho3=(放射组)findViewById(R.id.choice3);
cho3.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(RadioGroup组,int checkedId){
//TODO自动生成的方法存根
开关(检查EDID){
案例R.id.A3:
P3=“1”;
打破
案例R.id.B3:
P3=“2”;
打破
案例R.id.C3:
P3=“3”;
打破
案例R.id.D3:
P3=“4”;
打破
}
}
});
cho4=(放射组)findViewById(R.id.choice4);
cho4.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(RadioGroup组,int checkedId){
//TODO自动生成的方法存根
开关(检查EDID){
案例R.id.A4:
P2=“1”;
打破
案例R.id.B4:
P2=“2”;
打破
案例R.id.C4:
P2=“3”;
打破
案例R.id.D4:
P2=“4”;
打破
}
}
});
cho5=(放射组)findViewById(R.id.choice5);
cho5.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(RadioGroup组,int checkedId){
//TODO自动生成的方法存根
开关(检查EDID){
案例R.id.A5:
P3=“1”;
打破
案例R.id.B5:
P3=“2”;
打破
案例R.id.C5:
P3=“3”;
打破
案例R.id.D5:
P3=“4”;
打破
}
}
});
idm=(TextView)findViewById(R.id.nim_mhs);
nama_mk=(TextView)findviewbyd(R.id.nama_mk);
kls=(文本视图)findViewById(R.id.kelas);
mhw_mk=(TextView)findViewById(R.id.id_mhw_mk);
提交=(按钮)findViewById(R.id.submit);
submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建EntryData().execute();
}
});
}
公共类EntryData扩展异步任务{
串成功;
字符串url_链接=”http://wwwww/xxxxx/yyyyy/zzzzz.php";
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串strnama=nama_mk.getText().toString();
字符串stremail=idm.getText().toString();
字符串strpassword=kls.getText().toString();
字符串strmhwmk=mhw_mk.getText().toString();
List nvp=new ArrayList();
<?php
$id_mhs=$_POST['idmahasiswa'];
$nama_mk = $_POST['matakuliah'];
$kelas=$_POST['kelas'];
$id_mhw_mk=$_POST['id_mhw_mk'];
$pert1=$_POST['p1'];
$pert2=$_POST['p2'];
$pert3=$_POST['p3'];
$pert4=$_POST['p4'];
$pert5=$_POST['p5'];
$periode=date('Y-m-d H:i:s');
include "koneksi.php";
$query = "INSERT INTO main_hasil_raw (idmahasiswa,matakuliah,kelas,id_mhw_mk,periode,p1,p2,p3,p4,p5)VALUES('$id_mhs','$nama_mk','$kelas','$id_mhw_mk','$periode','$pert1','$pert2','$pert3','$pert4','$pert5')";
$hasil = mysql_query($query);
if($hasil)
{
$response["success"] = "1";
$response["message"] = "Data sukses diinput";
echo json_encode($response);
}
else
{$response["success"] = "0";
$response["message"] = "Maaf , terjadi kesalahan";
// echoing JSON response
echo json_encode($response);
}
?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TextView
android:id="@+id/nim_mhs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView"
android:visibility="invisible" />
<TextView
android:id="@+id/kelas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Kelas" />
<TextView
android:id="@+id/nama_mk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:text="Nama Matakuliah"
android:textColor="#333333"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:padding="10dip"
android:text="1.Question One"/>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/choice1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" >
<RadioButton
android:id="@+id/A1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="1"/>
<RadioButton
android:id="@+id/B1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>
<RadioButton
android:id="@+id/C1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>
<RadioButton
android:id="@+id/D1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:padding="10dip"
android:text="2. Question Two"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/choice2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" >
<RadioButton
android:id="@+id/A2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="1"/>
<RadioButton
android:id="@+id/B2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>
<RadioButton
android:id="@+id/C2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>
<RadioButton
android:id="@+id/D2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"/>
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:padding="10dip"
android:text="3. Question Three"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/choice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" >
<RadioButton
android:id="@+id/A3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="1"/>
<RadioButton
android:id="@+id/B3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>
<RadioButton
android:id="@+id/C3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>
<RadioButton
android:id="@+id/D3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"/>
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:padding="10dip"
android:text="4. Question Four"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/choice4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" >
<RadioButton
android:id="@+id/A4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="1"/>
<RadioButton
android:id="@+id/B4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>
<RadioButton
android:id="@+id/C4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>
<RadioButton
android:id="@+id/D4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"/>
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:padding="10dip"
android:text="5. Question Five"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/choice5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4" >
<RadioButton
android:id="@+id/A5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="1"/>
<RadioButton
android:id="@+id/B5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>
<RadioButton
android:id="@+id/C5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>
<RadioButton
android:id="@+id/D5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"/>
</RadioGroup>
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<TextView
android:id="@+id/id_mhw_mk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
</TableLayout>
</ScrollView>