Android studio says volley.NoConnectionError: java

2019-08-20 11:32发布

I have a super basic register script in android studio. Im trying to get it to connect to a php file located in the Localhost of XAMMP. My question is unique because i directing my script to a specific localhost url, not searching on the internet. The URL is accurate too, unless it has to be changed BACK to being localhost(not ip). The error code is

E/Create User: com.android.volley.NoConnectionError: java.io.EOFException

here is the register request;

public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://(ip address):3306/testing/Register.php";
    private Map<String, String> params;
    public RegisterRequest(String username, String password,String isAdmin,
                           Response.Listener<String> listener,
                           Response.ErrorListener errListener){
        super(Method.POST, REGISTER_REQUEST_URL,listener,errListener);
        params = new HashMap<>();
        params.put("username",username);
        params.put("password",password);
        params.put("isAdmin",isAdmin+"");
    }

    public Map<String, String> getparams() {
        return params;
    }
}

here is the CreateUser class;

public class CreateUser extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_user);
        this.setTitle("Create User");
        final EditText username1 = findViewById(R.id.Createusername);
        final EditText password1 = findViewById(R.id.CreatePassword);
        final Switch isAdmin = findViewById(R.id.isadmin);
        final Button createuser = findViewById(R.id.createuserbtn);
        if (getIntent().hasExtra("com.example.northlandcaps.crisis_response")){
            isAdmin.setVisibility(View.GONE);
        }
        createuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String username = username1.getText().toString();
                final String password = password1.getText().toString();
                final String isadmin = isAdmin.getText().toString();
                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success){
                                Intent intent = new Intent(CreateUser.this, MainActivity.class);
                                startActivity(intent);
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry",null)
                                        .create()
                                        .show();


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener,errorListener);
                RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
                queue.add(registerRequest);
            }
        });
    }
    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_SHORT).show();
            Log.e("Create User", error+"");
        }
    };

The php file, like i said, is inside htdocs inside of XAMMP. both apache and mysql are running, however, someone said i may not have my php script invoked by apache. Or that php even enabled on my Apache server.

1条回答
Juvenile、少年°
2楼-- · 2019-08-20 12:26

try this

int socketTimeout = 500000;//30 seconds - change to what you want
            RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            stringRequest.setRetryPolicy(policy);
            // Creating RequestQueue.
            RequestQueue queue = Volley.newRequestQueue(CreateUser.this);

            // Adding the StringRequest object into requestQueue.
            queue.add(registerRequest);
查看更多
登录 后发表回答