weis gerade nicht wo der fehler liegt woltle mal testen ob die Daten in der Datenbank angelegt werden.
das ist der Code Nr1 in der mainactivity:
Sent sent=new Sent("dies ist eine test frage","dies ist eine test antwort");
dan die Sent klasse:
package com.example.test.heilpraktikerapp;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Sent {
private String frage;
private String antwort;
private String mBaseUrl="XXXXXXXXXXXXX(kanns irgendie hier nicht posten wegen spam).cXm/hp_app/add.php";
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
public JSONObject toJSONObj() {
JSONObject obj = new JSONObject();
try {
obj.put("frage", this.frage);
obj.put("antwort", this.antwort);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
public Sent(String frage, String antwort){
JSONObject obj = new JSONObject();
try {
obj.put("frage", frage);
obj.put("antwort", antwort);
} catch (JSONException e) {
e.printStackTrace();
}
String url = mBaseUrl; //+ "/" + id + "/report";
JSONObject json= obj;
String jsonString = json.toString();
RequestBody body = RequestBody.create(JSON, jsonString);
Request request = new Request.Builder()
.header("X-Client-Type", "Android")
.url(url)
.post(body)
.build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException throwable) {
throwable.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
}});}}
und noch der php Code ohne Passwort :
<?php
$con = mysqli_connect("localhost", "id3959215_heilpraktiker", "XXX", "id3959215_hp_app");
# Get JSON as a string
$json_str = file_get_contents('php://input');
# Get as an object
$json_obj = json_decode($json_str);
$frage=$json_obj->{'frage'};
$antwort=$json_obj->{'antwort'};
$statement = mysqli_prepare($con, "INSERT INTO test (frage, antwort) VALUES (?, ?)");
mysqli_stmt_bind_param($statement, "ss", $frage, $antwort);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.