Hi, I have an app with volley plus, I send a POST request to a file: text1.php and I have verified that the variable $ _POST receives something but does not receive the parameter that I send $ _POST ["data1"] through my app.
I save the result in a file: vardump.txt and write to me: a: 0: {} and I also save it using json_decode in vardumpDecode.txt 'it does not save anything.
From my app if I receive a reply: "POST exists"
If I use the GET method if it works well but I am interested in using POST.
Thank you very much for the help.
public class MainActivity extends AppCompatActivity {
TextView tvDato1;
RequestQueue request;
JsonObjectRequest jsonObjectRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDato1 = (TextView) findViewById(R.id.tvDato1);
request = Volley.newRequestQueue(getApplicationContext());
cargarWebServiceWeb();
}
private void cargarWebServiceWeb() {
String url ="http://192.168.1.10/blog-wp001/text1.php";
final String dato1 ="este es el dato1";
jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
url,
null,
createMyReqSuccessListener(),
createMyReqErrorListener()){
protected Map<String, String> getParams() throws com.android.volley.error.AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("dato1", dato1);
return params;
};
};
request.add(jsonObjectRequest);
}
private Response.Listener<JSONObject> createMyReqSuccessListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response.isNull("dato1")) {
Toast.makeText(getApplicationContext(), "No se encontró dato1 ", Toast.LENGTH_LONG).show();
return;
}
tvDato1.setText(response.optString("dato1"));
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "No se ha podido conectar con el servidor" + error, Toast.LENGTH_LONG).show();
Log.i("ERROR", error.toString());
}
};
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dato1"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvDato1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<?php
if (isset($_POST)){
$json["dato1"] = "existe POST";
$fp = fopen('vardump.txt', 'w');
fwrite($fp, serialize($_POST));
fclose($fp);
$fp = fopen('vardumpDecode.txt', 'w');
fwrite($fp, json_decode(serialize($_POST)));
fclose($fp);
}else{
$json["dato1"] = "no existe POST";
}
echo json_encode($json);
?>