单击登录按钮时系统会抛出超时错误

时间:2017-12-23 00:02:11

标签: java android-volley

在我的android java应用程序中,来自LoginActivity我正在调用“LoginRegisterWebService”restful webservice,但点击登录按钮 系统抛出“发生一些错误> com.android.volley.TimeOutError”。这里我使用的是Remix OS播放器模拟器。已经通过几个与排球超时错误相关的答案并增加了超时时间, 但它没有任何区别。在此步骤中设置断点'public void onResponse(String s)',但系统不在onResponse内部。我也关掉了防火墙。 请有人帮我解决问题 enter image description here 请找到我的Gradle设置

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.foodies.myfoodies"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.volley:volley:1.0.0'
    testCompile 'junit:junit:4.12'
}

// LogingActivity代码在下面给出

public class LoginActivity extends AppCompatActivity {
    EditText emailBox, passwordBox;
    Button loginButton;
    TextView registerLink;
    String URL = "http://[ip address]:8081/MyFoodies/LoginRegisterWebService/login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        emailBox = (EditText)findViewById(R.id.emailBox);
        passwordBox = (EditText)findViewById(R.id.passwordBox);
        loginButton = (Button)findViewById(R.id.loginButton);
        registerLink = (TextView)findViewById(R.id.registerLink);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
                    @Override
                    public void onResponse(String rs) {
                        if(rs.equals("true")){
                            Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(LoginActivity.this,Home.class));
                        }
                        else{
                            Toast.makeText(LoginActivity.this, "Incorrect Details", Toast.LENGTH_LONG).show();
                        }
                    }
                },new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(LoginActivity.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> parameters = new HashMap<String, String>();
                        parameters.put("email", emailBox.getText().toString());
                        parameters.put("password", passwordBox.getText().toString());
                        return parameters;
                    }
                };

                request.setRetryPolicy(new DefaultRetryPolicy(
                        7000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                RequestQueue rQueue = Volley.newRequestQueue(LoginActivity.this);
                int socketTimeout = 10000;//10 seconds - change to what you want
                RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
                request.setRetryPolicy(policy);
                rQueue.add(request);

            }
        });

        registerLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });


    }
}

//以下是Webservice.java代码

@Path("/LoginRegisterWebService")
public class LoginRegisterWebService {

    final static String url = "jdbc:mysql://localhost:3307/foodhub";
    final static String user = "root";
    final static String pass = "root";

    @POST
    @Path("/login")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_HTML)
    public String login(@FormParam("email") String email, @FormParam("password") String password){
        String result="false";

        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, pass);

            PreparedStatement ps = con.prepareStatement("select * from foodhub.login where email=? and UserPassword=?");
            ps.setString(1, email);
            ps.setString(2, password);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                result = "true";
            }

            con.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return result;
    }

    @POST
    @Path("/register")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_HTML)
    public String register(@FormParam("email") String email, @FormParam("password") String password){
        String result="false";
        int x = 0;

        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, pass);

            PreparedStatement ps = con.prepareStatement("insert into login(email, UserPassword) values(?,?)");
            ps.setString(1, email);
            ps.setString(2, password);

            x = ps.executeUpdate();

            if(x==1){
                result = "true";
            }

            con.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

可能有多种原因造成这种情况。

  1. API服务器是否在您指定的端口上运行?
  2. 您的IP是否可以访问IP地址?
  3. API执行脚本执行/发送响应的时间太长了?网络服务器具有最大超时限制。
相关问题