2015-04-07 32 views
0

我將從mysql檢索到的數據設置爲TextView,現在我需要從「有價值的textview」中插入值。我認爲我需要2個php/api來做到這一點,但我不知道如何在一個活動中使用它。它成功檢索到該值並將其設置爲TextView,但未能將其發送到mysql。如何將有價值的textviews中的值插入到mysql

這裏是我的腳本:

public class ListJawaban extends Activity { 
    JSONArray mk = null; 


    ProgressDialog pDialog; 
    TextView idm, nama_mk, kls; 
    Button submit; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.spin_main); 

      Intent in = getIntent(); 
      String kode = in.getStringExtra("id_stu"); 
      String link_url = "http://xxxxxxxx/ffffff/gggggg/mkQuiz.php?kode="+kode; 

      JSONParser jParser = new JSONParser(); 

      JSONObject json = jParser.AmbilJson(link_url); 

      try { 
       mk = json.getJSONArray("mk"); 

       for(int i = 0; i < mk.length(); i++){ 
        JSONObject ar = mk.getJSONObject(i); 

        TextView mhs = (TextView) findViewById(R.id.nim_mhs); 
        TextView mkname = (TextView) findViewById(R.id.nama_mk); 
        TextView kelas = (TextView) findViewById(R.id.kelas); 

        String mhs_get = ar.getString("id_mhw"); 
        String mkname_get = ar.getString("mk_mhw"); 
        String kelas_get = ar.getString("kelas"); 

        mhs.setText(mhs_get); 
        mkname.setText(mkname_get); 
        kelas.setText(kelas_get); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
submit = (Button) findViewById(R.id.submit); 
      submit.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 


         new DataMasuk().execute(); 
         Intent forum = new Intent(getApplicationContext(), ForumActivity.class);      
         forum.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(forum);     
         finish(); 
       } 
      }); 
     } 

     public class DataMasuk extends AsyncTask<String, String, String> { 

      String success; 

      String url_link = "http://xxxxxxxx/ffffff/gggggg/postAns.php"; 

      @Override 
      protected String doInBackground(String... params) { 

       String strnama = nama_mk.getText().toString(); 
       String stremail = idm.getText().toString(); 
       String strpassword = kls.getText().toString(); 

       List<NameValuePair> nvp = new ArrayList<NameValuePair>(); 
       nvp.add(new BasicNameValuePair("mk_mhs", strnama)); 
       nvp.add(new BasicNameValuePair("id_mhs", stremail)); 
       nvp.add(new BasicNameValuePair("kelas", strpassword)); 

       JSONParser jParser = new JSONParser(); 

       JSONObject json = jParser.makeHttpRequest(url_link, "POST", nvp); 
       try { 
        success = json.getString("success"); 

       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), "Error", 
          Toast.LENGTH_LONG).show(); 
       } 

       return null; 
      } 



     } 
} 

,這是插入值的PHP

<?php 

$id_mhs=$_POST['id_mhs']; 
$mk_mhs = $_POST['mk_mhs']; 
$kelas=$_POST['kelas']; 

include "koneksi.php"; 


$query = "INSERT INTO tbl_tampung (id_mhs,mk_mhs,kelas)VALUES('$id_mhs','$mk_mhs','$kelas')"; 
$hasil = mysql_query($query); 
if($hasil) 
    { 
    $response["success"] = "1"; 
     $response["message"] = "Successfully Added"; 
     echo json_encode($response); 
    } 
    else 
    {$response["success"] = "0"; 
    $response["message"] = "Sorry somethings wrong"; 

     echo json_encode($response); 
    } 
?> 

,但它不工作,任何人都可以幫我嗎?它的功課,並應在週四進行:(

+0

刪除意圖論壇=新意圖(getApplicationContext(),ForumActivity.class); 論壇。 addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(論壇); 完成();並開始ForumActivity如果更迭中doinbackground – eurosecom

+0

感謝先生= 1,它的工作原理:) –

回答

1

嘗試改變

 submit = (Button) findViewById(R.id.submit); 
     submit.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 


        new DataMasuk().execute(); 

      } 
     }); 

 @Override 
     protected String doInBackground(String... params) { 

      String strnama = nama_mk.getText().toString(); 
      String stremail = idm.getText().toString(); 
      String strpassword = kls.getText().toString(); 

      List<NameValuePair> nvp = new ArrayList<NameValuePair>(); 
      nvp.add(new BasicNameValuePair("mk_mhs", strnama)); 
      nvp.add(new BasicNameValuePair("id_mhs", stremail)); 
      nvp.add(new BasicNameValuePair("kelas", strpassword)); 

      JSONParser jParser = new JSONParser(); 

      JSONObject json = jParser.makeHttpRequest(url_link, "POST", nvp); 
      try { 
       success = json.getInt("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; 
     } 



    } 
+0

與此'成功= json.getString(「成功」);如果(成功==「1」){'它工作正常。感謝您的幫助先生:) –