gpt4 book ai didi

android - org.apache.http.conn.HttpHostConnectException : Connection to http://localhost refused in android

转载 作者:IT老高 更新时间:2023-10-28 21:58:13 27 4
gpt4 key购买 nike

我正在制作一个应用程序,该应用程序在执行时将图像上传到服务器并将其数据库更新到 android 中的服务器,它显示错误

Connection to http://localhost refused 

还有更多错误,我研究了这个问题,发现没有提供 URL 连接

http://localhost/... provide the link as http://10.0.2.2/... 

我也这样做了,但问题是一样的更多错误在这里:

E/org.apache.http.conn.HttpHostConnectException(4318): Connection to http://localhost refused
E/org.apache.http.conn.HttpHostConnectException(4318): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/org.apache.http.conn.HttpHostConnectException(4318): at com.example.android.photobyintent.ViewRecipients.uploadFile(ViewRecipients.java:325)
E/org.apache.http.conn.HttpHostConnectException(4318): at com.example.android.photobyintent.ViewRecipients$1.run(ViewRecipients.java:238)
E/org.apache.http.conn.HttpHostConnectException(4318): at java.lang.Thread.run(Thread.java:856)
E/org.apache.http.conn.HttpHostConnectException(4318): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
E/org.apache.http.conn.HttpHostConnectException(4318): at libcore.io.IoBridge.connect(IoBridge.java:114)
E/org.apache.http.conn.HttpHostConnectException(4318): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
E/org.apache.http.conn.HttpHostConnectException(4318): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
E/org.apache.http.conn.HttpHostConnectException(4318): at java.net.Socket.connect(Socket.java:842)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
E/org.apache.http.conn.HttpHostConnectException(4318): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
E/org.apache.http.conn.HttpHostConnectException(4318): ... 8 more
E/org.apache.http.conn.HttpHostConnectException(4318): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
E/org.apache.http.conn.HttpHostConnectException(4318): at libcore.io.Posix.connect(Native Method)
E/org.apache.http.conn.HttpHostConnectException(4318): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
E/org.apache.http.conn.HttpHostConnectException(4318): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
E/org.apache.http.conn.HttpHostConnectException(4318): at libcore.io.IoBridge.connect(IoBridge.java:112)
E/org.apache.http.conn.HttpHostConnectException(4318): ... 13 more

文件上传代码为:

public int uploadFile(ArrayList<String> sourceFileUri, String info, String latitude, String longitude, String id) throws IOException {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://10.0.2.2/deliverysystem/order/add");

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

entity.addPart("returnformat", new StringBody("json"));
System.out.println(sourceFileUri.size());
for(int i=0;i<sourceFileUri.size();i++){
String sourceFile = sourceFileUri.get(i);
entity.addPart("uploaded_file"+(i+1), new FileBody(new File(sourceFile)));
}

entity.addPart("fld_delivery_id", new StringBody(id));
entity.addPart("fld_delivery_location", new StringBody(info));
entity.addPart("fld_latitude", new StringBody(latitude));
entity.addPart("fld_longitude", new StringBody(longitude));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));

String sResponse = reader.readLine();
return 1;
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();

Log.e(e.getClass().getName(), e.getMessage(), e);
return 1;
}

}

在调试代码时达到:

httpPost.setEntity(entity);

它在此行之后显示异常,即在此语句上:

HttpResponse response = httpClient.execute(httpPost, localContext);

谁能帮我解决这个问题?

最佳答案

如果您使用模拟器为本地服务器运行您的应用程序。将本地 ip 提及为 10.0.2.2 并且必须授予 Internet 权限进入您的应用:

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

关于android - org.apache.http.conn.HttpHostConnectException : Connection to http://localhost refused in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com