单选按钮不断发送错误的值

时间:2015-04-14 08:10:10

标签: php android

我是Android的新手,我最近有一个功课:其中一个活动会将单选按钮和一些文本视图的值发送到数据库,但是当我尝试运行它并检查我的数据库时,它会一直显示错误值。发送正确值的唯一单选按钮是第一个。

这是活动:

@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;
        }

    }

PHP代码:

<?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>

你能帮我/教我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

cho4cho5复选框中,您重复使用变量P2P3,这是故意的吗?

(我会把它作为评论,但我不具备所需的声誉)