新的AsyncHttpResponseHandler()总是会失败

时间:2016-10-06 09:26:31

标签: android asynchronous

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();    
params.put("username", username);
params.put("password", password);
client.post("192.168.12.4/administration/include/login.php", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println("onSuccess");
            Toast.makeText(getApplicationContext(), "MySQL DB has been informed about Sync activity", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(int statusCode, Throwable error, String content) {
            System.out.println("onFailure");
            Toast.makeText(getApplicationContext(), "Error Occured", Toast.LENGTH_LONG).show();
        }
    });

如果我有上面的代码,可能会导致它转到public void onFailure(int statusCode, Throwable error, String content) {}而不是public void onSuccess(String response) {}

其中192.168.12.4是运行localhost的计算机的IP以及包含login.php的项目所在的位置

2 个答案:

答案 0 :(得分:1)

将“http or 'https等协议添加到URL

更改URL

192.168.12.4/administration/include/login.php

http://192.168.12.4/administration/include/login.php

答案 1 :(得分:1)

如果这是开发人员计算机(运行模拟器的PC)进行模拟器测试,则必须将URL值更改为

http://10.0.2.2/administration/include/login.php

不要忘记在清单文件中添加它

<uses-permission android:name="android.permission.INTERNET" />