Json, PHP + Android Studio

0

I'm making an application where the user types his "name" and calls json

Android code

 btn_Enviar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject jo = new JSONObject();
            try {
                jo.put("status", nome.getText());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            callWS("send-json", jo.toString());
        }
    });
}

public void callWS(final String method, final String data) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            answer = HttpConnection.getSetDataWeb(Config.url, method, data);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.i(TAG, "." + answer.substring(4) + ". - ." + data + ".");
                    Log.i("poo", answer + ". - ." + data);
                    if (data.equals(answer)) {
                        Toast.makeText(getApplicationContext(), "Successfully ;)", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Failed! :(", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }).start();
}

PHP code, where I get the data is already registering in the DB all right.

if(strcmp('send-json', $_POST['method']) == 0){

    $data = $_POST['json'];
    $data = json_decode($data,true);

    $status = $data['status'];

    $query_1 = "INSERT INTO eletrica(status) VALUES ('$status')";
    $query_2 = mysqli_query($conn, $query_1);

    if(mysqli_affected_rows($conn) != 0){
        //echo("OKAY");
    }else{    
        //echo("FAILED");
    }

    echo ("0");
}

I think I'm not sure how to return the correct answer from the server to android, because it's giving this error in the android studio logcat, the FAILED message appears but anyway it registers the edit text value in BD

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.joaotoga.arduino, PID: 19343
                                                                          java.lang.StringIndexOutOfBoundsException: length=1; index=4
                                                                              at java.lang.String.indexAndLength(String.java:294)
                                                                              at java.lang.String.substring(String.java:1067)
                                                                              at com.example.joaotoga.arduino.MainActivity$2$1.run(MainActivity.java:51)
                                                                              at android.os.Handler.handleCallback(Handler.java:746)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Line 51 is this one:

Log.i(TAG, "." + answer.substring(4) + ". - ." + data + ".");
    
asked by anonymous 14.06.2018 / 19:28

0 answers