【发布时间】:2018-01-23 18:48:26
【问题描述】:
我正在编写一个 android 应用程序,我想将一些字符串数据插入到我的 mysql 数据库中。我的 MainActivity 中有一个 CheckBox。如果选中,我想在我的 mysql-databse 中插入“是”,如果未选中,我想插入“否”。目前它不起作用。我认为我的主要问题是,它说 str_cb_gesdeutsch 从未使用过。为什么我不能在“public void OnAnghinzu”中使用它?
MainActivity.java
public class MainActivity extends AppCompatActivity {
CheckBox cb_gesdeutsch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anghinzu);
cb_gesdeutsch = (CheckBox) findViewById(R.id.cb_gesdeutsch);
}
public void CheckboxUpload() {
boolean cb_checked = ((CheckBox) cb_gesdeutsch).isChecked();
if(cb_checked){
Toast.makeText(AnghinzuActivity.this,"Checkbox is checked",Toast.LENGTH_SHORT).show();
String str_cb_gesdeutsch = "Yes";
} else {
Toast.makeText(AnghinzuActivity.this,"Checkbox is unchecked",Toast.LENGTH_SHORT).show();
String str_cb_gesdeutsch = "No";
}
}
public void OnAnghinzu(View view) {
CheckboxUpload();
String type = "add";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, str_cb_gesdeutsch);
}
}
BackgroundWorker.java
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx) {
context = ctx;
}
if(type.equals("add")) {
try {
String gesdeutsch = params[1];
URL url = new URL("*******");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("Gesamtdeutschland", "UTF-8")+"="+URLEncoder.encode(gesdeutsch, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));//iso-8859-1
String result = "";
String line = "";
while((line = bufferedReader.readLine()) != null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
}
【问题讨论】:
标签: java android mysql checkbox