I am developing Android Studio front-end and Springboot back-end at the same time.
Getting Springboot to run as localhost,
I'm building and running on a physical smartphone device by connecting to an Android studio, and I don't know how to connect these to rest API.
I wrote the front-end code as below,
`com.android.volley.TimeoutError ` error occurs whether it is not connecting well with this local server.
I'd like to ask for your advice on how to do it right.
Url's 192.168.56.1 is my ipv4 address that comes out after typing ipconfig in Windows terminal.
Thank you.
public class JoinActivity extends AppCompatActivity {
private EditText editText_email;
private TextView text_message;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_join);
editText_email = findViewById(R.id.editText_email);
Button btn_dupCheck = findViewById(R.id.btn_dupCheck);
text_message = findViewById(R.id.text_message);
queue = Volley.newRequestQueue(this);
String url="http://192.168.56.1:8080/user/check-email";
btn_dupCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JSONObject jsonRequest = new JSONObject();
try {
jsonRequest.put("email", editText_email.getText().toString());
Log.d("success", editText_email.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, jsonRequest, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("success", response.toString());
try {
boolean isSuccess = response.getBoolean("isSuccess");
if (isSuccess) {
JSONObject result = response.getJSONObject("result");
boolean emailExists = result.getBoolean("emailExists");
if (emailExists) {
text_message.setTextColor(getResources().getColor(R.color.red));
text_message.setText("dup email");
} else {
text_message.setTextColor(getResources().getColor(R.color.green));
text_message.setText("available email");
}
} else {
Toast.makeText(JoinActivity.this, "server res error", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(JoinActivity.this, "JSON parsing error", Toast.LENGTH_SHORT).show();
}
// Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
if (error.networkResponse != null && error.networkResponse.data != null) {
try {
String errorResponse = new String(error.networkResponse.data, "utf-8");
JSONObject jsonObject = new JSONObject(errorResponse);
String errorMessage = jsonObject.getString("errorMessage");
// Handle BaseException
Log.d("test", "BaseException: " + errorMessage);
} catch (UnsupportedEncodingException | JSONException e) {
e.printStackTrace();
}
}
}
});
request.setShouldCache(false);
request.setRetryPolicy(new DefaultRetryPolicy(100000000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(request);
}
});
}
}
I want to make it work well on the physical device even if I run it on a local server.
The problem with the local server- it's probably the network itself. If you're testing on a physical device, make sure they're on the same wifi network and make sure your router is setup to allow traffic to that port and machine from other devices on the same wifi network (by default this is normally blocked on commercial routers). If it's being tested on an emulator on the same machine, use 10.0.2.2 instead, that's an IP that android emulators map to the host machine.
Please note that it will not work at all on a physical device unless you have a publicly routable IP address (which basically means hosting it in the cloud or running your own datacenter) or are on the same wifi network. It will never work from a locally hosted server if on the cellular network, because the cellular network will not be able to find your IP (192.168.56.x isn't a real IP, it's only accessible from the same network).