我将从 mysql 检索到的数据设置为 TextView
,现在我需要从“有值(value)的 TextView ”插入值。我想我需要 2 个 php/api 来做到这一点,但我不知道如何在一项 Activity 中使用它。它成功检索到该值并将其设置为TextView
,但无法将其发送到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);
}
?>
但是不行,谁能帮帮我?这是我的作业,应该在星期四完成:(
最佳答案
尝试改变
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;
}
}
关于php - 如何将有值(value)的 TextView 中的值插入到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495991/