我正在编写一个Android应用程序,我想在我的mysql数据库中插入一些String数据。我的MainActivity中有一个CheckBox。如果选中,我想插入"是"到我的mysql-databse,如果未选中,我想插入" No"。当然它不起作用。我认为我的主要问题是,它说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();
}
}
&#13;
答案 0 :(得分:0)
只需复制并粘贴下面的代码就可以了。
public class MainActivity extends AppCompatActivity {
CheckBox cb_gesdeutsch;
String str_cb_gesdeutsch = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb_gesdeutsch = (CheckBox) findViewById(R.id.cb_gesdeutsch);
}
public void OnAnghinzu(View view) {
CheckboxUpload(view);
}
public void CheckboxUpload(View view) {
boolean cb_checked = ((CheckBox) cb_gesdeutsch).isChecked();
if (cb_checked) {
str_cb_gesdeutsch = "Yes";
Toast.makeText(MainActivity.this, "Checkbox is checked " + str_cb_gesdeutsch, Toast.LENGTH_SHORT).show();
} else {
str_cb_gesdeutsch = "No";
Toast.makeText(MainActivity.this, "Checkbox is unchecked " + str_cb_gesdeutsch, Toast.LENGTH_SHORT).show();
}
}
}