简体   繁体   中英

Android Volley works well on device but fail to work in emulator

I realize something very strange about volley. I testing a register function on Android. Initially, when i click on the button, it always no response(no Toast, no error prompt) . But when i put in Log and check in the logcat when running the apps, all the data processed correctly. And guess what? when i upload all my php files into server, install the apps on Phone and it works perfectly!

By right, what I care is that it should work on Device. But I am just wondering why it doesn't work in Emulator as most of time i will be testing on Emulator.

Main4Activity.java

public class Main4Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    final EditText username=(EditText)findViewById(R.id.username);
    final EditText email=(EditText)findViewById(R.id.email);
    final EditText password=(EditText)findViewById(R.id.password);

    final Button submit=(Button)findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final String get_username=username.getText().toString();
            final String get_email=email.getText().toString();
            final String get_password=password.getText().toString();

            Log.d("username","->"+get_username);
            Log.d("email","->"+get_email);
            Log.d("password","->"+get_password);

            Response.Listener<String> response_listener=new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try{
                        JSONObject jsonObject=new JSONObject(response);
                        boolean success=jsonObject.getBoolean("success");

                        if(success){

                            Toast.makeText(Main4Activity.this,"Reg Done!",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(Main4Activity.this,"Reg FAILED!",Toast.LENGTH_SHORT).show();

                        }

                    }catch(JSONException e){
                        e.printStackTrace();;

                    }
                }
            };
            register_request register_request=new register_request(get_username,get_email,get_password,response_listener);
            RequestQueue queue=Volley.newRequestQueue(Main4Activity.this);
            queue.add(register_request);
        }
    });
}
}

register_request.java

public class register_request extends StringRequest {

private static final String url="http://abcdefg.com";
private Map<String,String> params;
public register_request(String username,String email,String password, Response.Listener<String> listener) {
    super(Method.POST,url, listener, null);
    params=new HashMap<>();
    params.put("username",username);
    params.put("email",email);
    params.put("password",password);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

Anyone has this problem before?

I recently had a similar problem. For me, the solution was to change the server value from " http://localhost:8080 " to " http://192.168.1.x:8080 " (the static ip of my workstation). In your example I see you put

private static final String url="http://abcdefg.com";

which is obviously a random url, so I really think you're doing the same thing I was doing recently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM