AsyncHttpClient发布请求连接被拒绝

时间:2017-03-29 11:09:57

标签: android android-asynctask

我目前遇到关于asynchttpclient帖子请求的问题。我能够使用loopj的asynchttpclient执行get请求,但我遇到了关于post请求的问题。

这是我的帖子请求的代码。

 AsyncHttpClient client = new AsyncHttpClient();
                RequestParams params = new RequestParams();
                params.put("username", username);
                params.put("password", password);

            client.post("http://localhost/lin/mobile_login/", params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    String response = new String(responseBody);
                    Log.d("RAW", String.valueOf(responseBody));
                    Log.d("RESPONSE",response);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    String err = new String(responseBody);
                    Log.d("ERROR", err);
                    Log.d("EXCE", String.valueOf(error));
                }
            });

然后抛出

 java.net.ConnectException: Connection refused
     at java.net.PlainSocketImpl.socketConnect(Native Method)
     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
     at java.net.Socket.connect(Socket.java:605)
     at cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
     at cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:157)
     at cz.msebera.android.httpclient.conn.scheme.SchemeSocketFactoryAdaptor.connectSocket(SchemeSocketFactoryAdaptor.java:65)
     at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
     at cz.msebera.android.httpclient.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:131)
     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
     at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
     at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
     at java.lang.Thread.run(Thread.java:761)

我试图将2个参数传递给我的帖子请求。希望你能帮助我解决我的问题。

1 个答案:

答案 0 :(得分:1)

问题在于您的网址localhost

Url中不能包含localhost。您必须在此处添加IP地址,而不是http://192.168.42.37/lin/mobile_login/,例如192.168.42.37,而 <div ng-controller="Controller1"> <div custom-performance="log1" content="Content1">{{log1}}</div> </div> <div ng-controller="Controller2"> <div custom-performance="log2" content="Content2">{{log2}}</div> </div> 将是IP地址。您需要先找到IP地址。还要确保服务器和移动设备在同一网络上。