I'm currently working on Android Studio. Just trying to make an app to register account and save data on database using LAMP stack.
The code I was using worked for localhost (I'm using XAMPP), it posted data to the database.
Howevers, when I change the URL to my EC2 machine on AWS, the app showing "java.io.Exception Unexpected End Of Stream on com.android.okhttp" and didn't post data to database. I tested and verify the URL is correct.
What else should I try for this problem ? Any help would be really appreciated !
Here is the app UI: Click to see image
Here is the code:
package com.example.androidphp;import androidx.appcompat.app.AppCompatActivity;import android.app.ProgressDialog;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.android.volley.AuthFailureError;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import org.json.JSONException;import org.json.JSONObject;import java.util.HashMap;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText editTextUserName, editTextEmail, editTextPassword; private Button buttonRegister; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextEmail = (EditText) findViewById(R.id.editTextEmail); editTextUserName = (EditText) findViewById(R.id.editTextUserName); editTextPassword = (EditText) findViewById(R.id.editTextPassword); buttonRegister = (Button) findViewById(R.id.buttonRegister); progressDialog = new ProgressDialog(this); buttonRegister.setOnClickListener(this); } private void registerUser(){ final String email = editTextEmail.getText().toString().trim(); final String username = editTextUserName.getText().toString().trim(); final String password = editTextPassword.getText().toString().trim(); progressDialog.setMessage("Registering User...."); progressDialog.show(); // class from volley StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.URL_REGISTER, new Response.Listener<String>() { @Override public void onResponse(String response) { progressDialog.dismiss(); try { JSONObject jsonObject = new JSONObject(response); Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show(); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { progressDialog.hide(); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> params = new HashMap<>(); params.put("username", username); params.put("email", email); params.put("password", password); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } @Override public void onClick(View v) { if(v == buttonRegister) registerUser(); }}