I am programming an android app and I want to insert some String data into my mysql database. There is a CheckBox in my MainActivity. If it is checked i want to insert "Yes" to my mysql-databse and if it is unchecked I want to insert "No". Curently it don't work. I think my main problem is, that it says str_cb_gesdeutsch is never used. Why I can`t use it in "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();
}
}
Aucun commentaire:
Enregistrer un commentaire