简体   繁体   English

Spring Rest Template Client:拒绝连接到http:// localhost:8080

[英]Spring Rest Template Client : Connection to http://localhost:8080 refused

This my Android Activity 这是我的Android活动

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

         pdialog=new ProgressDialog(this);

         query = getIntent().getExtras().getString("query");
         if(!TextUtils.isEmpty(query))
               new GetRestTask().execute(query);

}

private class GetRestTask extends AsyncTask<String, Void, ObjectExchanged>
{

    @Override
    protected void onPreExecute() {
        pdialog.setCancelable(true);
        pdialog.setMessage("Sending ....");
        pdialog.show();
    }

    @Override
    protected ObjectExchanged doInBackground(String... request) {
        Map<String, String> vars = new HashMap<String, String>();
      //   vars.put("query", ClientRestActivity.this.query);

        if(request != null && request.length > 0)
        {
            try
            {
                return ( getRestTemplate()).getForObject(URL, ObjectExchanged.class,query );

            }

            catch (RestClientException e) {

                e.printStackTrace();
                return null;

            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(ObjectExchanged result) {

        if(result == null )
        {
            Toast.makeText(getApplicationContext(), "Non trouvé ou erreur", Toast.LENGTH_SHORT).show();

            pdialog.dismiss();          
        }
        else
        {
            Intent intent = new Intent(getApplicationContext(), ShowResults.class);

            intent.putExtra("list_uri", result.response);
            pdialog.dismiss();
        }

    }
}


private RestTemplate getRestTemplate() {
    if(restTemplate == null)
    {
        restTemplate = new RestTemplate();
        MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
        List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate.getMessageConverters();
        listHttpMessageConverters.add(jsonConverter);
        restTemplate.setMessageConverters(listHttpMessageConverters);
    }
    return restTemplate;
}

And this is my Servelet in server Side 这是我在服务器端的Servelet

protected void doGet(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException {

    String query= request.getParameter("query");

    /*System.out.println(query);
    PrintWriter out2 = response.getWriter();
    out2.println("<LI> Parametre 1: " + query); 
    */

    System.out.println(query);
    PrintWriter out2 = response.getWriter();

    out2.println("done ");

    ArrayList<URI> uris= new ArrayList<>();

    try {
        Scanner s= new Scanner(new File ("urls.txt"));
        while (s.hasNext()) {
            uris.add(new URI(s.nextLine()));

        }
    } catch (Exception e) {
        // TODO: handle exception
    }
    ObjectExchanged objectExchanged = new ObjectExchanged(query,uris);


    if (objectExchanged.equals(null)) {
        System.out.println("null");
    }
    else 
        System.out.println("not null");

    Writer out =response.getWriter();
    String repJson = new Gson().toJson(objectExchanged);
    System.out.println(repJson);
    out.write(repJson);
}

I try to run this on my own device 我尝试在自己的设备上运行

This is the log 这是日志

06-17 21:36:23.720: W/System.err(3010): org.springframework.web.client.ResourceAccessException: I/O error: Connection to http://localhost:8080 refused; nested exception is org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8080 refused
06-17 21:36:23.720: W/System.err(3010):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:456)
06-17 21:36:23.720: W/System.err(3010):     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
06-17 21:36:23.720: W/System.err(3010):     at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:202)
06-17 21:36:23.720: W/System.err(3010):     at com.pfe.client.webservice.ClientRestActivity$GetRestTask.doInBackground(ClientRestActivity.java:74)
06-17 21:36:23.720: W/System.err(3010):     at com.pfe.client.webservice.ClientRestActivity$GetRestTask.doInBackground(ClientRestActivity.java:1)
06-17 21:36:23.720: W/System.err(3010):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-17 21:36:23.720: W/System.err(3010):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:3        06)
06-17 21:36:23.720: W/System.err(3010):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-17 21:36:23.720: W/System.err(3010):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-17 21:36:23.730: W/System.err(3010):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-17 21:36:23.730: W/System.err(3010):     at java.lang.Thread.run(Thread.java:1019)
06-17 21:36:23.730: W/System.err(3010): Caused by: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8080 refused
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-17 21:36:23.730: W/System.err(3010):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-17 21:36:23.730: W/System.err(3010):     at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:82)
06-17 21:36:23.730: W/System.err(3010):     at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52)
06-17 21:36:23.740: W/System.err(3010):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:441)
06-17 21:36:23.740: W/System.err(3010):     ... 10 more
06-17 21:36:23.740: W/System.err(3010): Caused by: java.net.ConnectException: /127.0.0.1:8080 - Connection refused
06-17 21:36:23.740: W/System.err(3010):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
06-17 21:36:23.740: W/System.err(3010):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
06-17 21:36:23.750: W/System.err(3010):     at java.net.Socket.connect(Socket.java:983)
06-17 21:36:23.750: W/System.err(3010):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-17 21:36:23.750: W/System.err(3010):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
06-17 21:36:23.750: W/System.err(3010):     ... 19 more

localhost redirects calls by your router back to 127.0.0.1, which is the same device. 本地主机将路由器重定向的呼叫重定向回127.0.0.1,这是同一设备。 So unless your server is on the phone, this will cause a crash. 因此,除非您的服务器在电话上,否则将导致崩溃。 If you have a server that is connected to the same router network(wifi or lan) as your phone, then get the local IP of the server from your router or server and replace localhost with it. 如果您的服务器与电话连接到相同的路由器网络(wifi或lan),请从路由器或服务器获取服务器的本地IP,然后将其替换为localhost。

Note: phone and server must be connected to the same router 注意:电话和服务器必须连接到同一路由器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.web.client.ResourceAccessException:“http://localhost:8080/”的 GET 请求上的 I/O 错误:连接被拒绝:连接 - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/": Connection refused: connect Spring Rest模板Http客户端错误异常 - Spring Rest Template Http client error exception 在 IIS 服务器上运行的 Angular FE:对 http://localhost:8080/api 的 POST 请求生成 net::ERR_CONNECTION_REFUSED - Angular FE running on IIS server: POST request to http://localhost:8080/api generates net::ERR_CONNECTION_REFUSED Spring 在 localhost 上启动应用程序:连接被拒绝 - Spring Boot app on localhost: connection refused 代理下 HTTP 客户端 4.5 的 Rest 模板的连接超时 - Connection Timeout for Rest Template with HTTP Client 4.5 under Proxy Spring LocaleResolver不要放入startPage(http:// localhost:8080 /)defaultLocale - Spring LocaleResolver don't put to the startPage(http://localhost:8080/) defaultLocale org.apache.http.conn.HttpHostConnectException:拒绝连接到http:// localhost - org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused 无法执行 HTTP 请求:连接到 localhost:8000 [localhost/127.0.0.1] 失败:连接被拒绝(连接被拒绝) - Unable to execute HTTP request: Connect to localhost:8000 [localhost/127.0.0.1] failed: Connection refused (Connection refused) tomcat10主页无法通过键入localhost加载:8080错误:ERR_CONNECTION_REFUSED - tomcat10 home page can not load by typing localhost:8080 error: ERR_CONNECTION_REFUSED Android Studio错误org.apache.http.conn.HttpHostConnectException:拒绝与http://10.0.2.2:8080的连接 - Android Studio error org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM