[英]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.