java.net.ConnectException:无法连接到 /10.0.0.2(端口 80):连接失败:ETIMEDOUT(连接超时)
Posted
技术标签:
【中文标题】java.net.ConnectException:无法连接到 /10.0.0.2(端口 80):连接失败:ETIMEDOUT(连接超时)【英文标题】:java.net.ConnectException: failed to connect to /10.0.0.2 (port 80): connect failed: ETIMEDOUT (Connection timed out) 【发布时间】:2016-01-02 10:22:30 【问题描述】:尝试从我的笔记本电脑上运行的 XAMPP 服务器获取 JSON 数据,这只是一个测试应用程序,没有重要数据,我有一个主要活动,一个 HttpManager
用于获取数据的类,一个 UserdataJson
我用来解析数据,我有一个UserData
POJO,代码如下。
主活动:
public class MainActivity extends AppCompatActivity
TextView output;
ProgressBar pb;
Button btn;
List<UserData> userDataList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output=(TextView) findViewById(R.id.textView);
output.setMovementMethod(new ScrollingMovementMethod());
pb= (ProgressBar) findViewById(R.id.progressBar);
pb.setVisibility(View.INVISIBLE);
btn= (Button) findViewById(R.id.button);
onClicked();
public void onClicked()
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (isOnline())
requestData("http://10.0.0.2/database/getInfoDroid.php");
else
Toast.makeText(MainActivity.this, "Network Unavailable", Toast.LENGTH_LONG).show();
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
return super.onOptionsItemSelected(item);
private void requestData(String uri)
MyTask task=new MyTask();
task.execute(uri);
protected void updateDisplay()
if (userDataList!=null)
for (UserData userdata:userDataList
)
output.append(userdata.getName() + "\n");
protected boolean isOnline()
ConnectivityManager cm= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netinfo= cm.getActiveNetworkInfo();
if(netinfo!= null && netinfo.isConnectedOrConnecting())
return true;
else
return false;
private class MyTask extends AsyncTask
@Override
protected void onPreExecute()
// updateDisplay("Background Task Started");
pb.setVisibility(View.VISIBLE);
@Override
protected Object doInBackground(Object[] objects)
String content =HttpManager.getData(objects[0].toString());
return content;
@Override
protected void onPostExecute(Object o)
userDataList= UserDataJsonParser.parseFeed(o.toString());
updateDisplay();
pb.setVisibility(View.INVISIBLE);
@Override
protected void onProgressUpdate(Object[] values)
//updateDisplay(values[0].toString());
HttpManager:
public class HttpManager
public static String getData(String uri)
StringBuilder sb=new StringBuilder();
BufferedReader reader=null;
try
URL url=new URL(uri);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line=reader.readLine())!=null)
sb.append(line+ "\n");
return sb.toString();
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
if (reader!=null)
try
reader.close();
catch (IOException e)
e.printStackTrace();
return sb.toString();
JsonParser 类:
public class UserDataJsonParser
public static List<UserData> parseFeed(String content)
System.out.println(content);
try
JSONArray array = new JSONArray(content);
List<UserData> dataList= new ArrayList<>();
JSONObject obj=null;
for (int i=0;i<array.length();i++)
obj = array.getJSONObject(i);
UserData userData = new UserData();
userData.setId(obj.getInt("id"));
userData.setName(obj.getString("name"));
userData.setSurname(obj.getString("surname"));
userData.setUsername(obj.getString("username"));
userData.setPassword(obj.getString("password"));
dataList.add(userData);
return dataList;
catch (JSONException e)
e.printStackTrace();
return null;
正在尝试解析的 JSON 数据:
["id":"1","name":"Hilary","surname":"Mwape","username":"Hilz","password":"milan","id":"2","name":"John","surname":"Kalyondo","username":"jk","password":"jk92","id":"3","name":"Sipiwe","surname":"Tembo","username":"ST","password":"ST91"]
LOGCAT:
java.net.ConnectException: failed to connect to /10.0.0.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at java.net.Socket.connect(Socket.java:884)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
10-05 17:19:38.770 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.Connection.connect(Connection.java:152)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at hilz.myapplication.HttpManager.getData(HttpManager.java:22)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at hilz.myapplication.MainActivity$MyTask.doInBackground(MainActivity.java:122)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
10-05 17:19:38.771 12022-14516/hilz.myapplication W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at java.lang.Thread.run(Thread.java:818)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at libcore.io.Posix.connect(Native Method)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
10-05 17:19:38.772 12022-14516/hilz.myapplication W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)
10-05 17:19:38.773 12022-14516/hilz.myapplication W/System.err: ... 23 more
10-05 17:19:38.810 12022-12022/hilz.myapplication W/System.err: org.json.JSONException: End of input at character 0 of
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at org.json.JSONArray.<init>(JSONArray.java:92)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at hilz.myapplication.UserDataJsonParser.parseFeed(UserDataJsonParser.java:15)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at hilz.myapplication.MainActivity$MyTask.onPostExecute(MainActivity.java:133)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:651)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
10-05 17:19:38.811 12022-12022/hilz.myapplication W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
10-05 17:19:38.812 12022-12022/hilz.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
10-05 17:19:38.812 12022-12022/hilz.myapplication W/System.err: at android.os.Looper.loop(Looper.java:148)
10-05 17:19:38.812 12022-12022/hilz.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-05 17:19:38.812 12022-12022/hilz.myapplication W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-05 17:19:38.812 12022-12022/hilz.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-05 17:19:38.812 12022-12022/hilz.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-05 19:04:43.849 12022-12030/? I/art: Ignoring second debugger -- accepting and dropping
【问题讨论】:
你的 logcat 说有一个超时错误。您的应用无法连接到服务器。您确定服务器正在运行并且您使用的是正确的 url 和端口号吗? Server is Running am Sure url is correct 端口号与其说是初学者 你能检查一下这个命令的输出吗:telnet 10.0.0.2 80 【参考方案1】:终于找到了解决方案,每个人都说使用 10.0.0.2 但这在我的 URL 中给出了上述错误,但使用我的 PC 实际 ip n 就像一个魅力
【讨论】:
以上是关于java.net.ConnectException:无法连接到 /10.0.0.2(端口 80):连接失败:ETIMEDOUT(连接超时)的主要内容,如果未能解决你的问题,请参考以下文章
java.net.ConnectException: Connection refused
java.net.ConnectException:连接被拒绝
Tomcat:java.net.ConnectException:连接被拒绝
Hadoop:java.net.ConnectException:连接被拒绝
Spring Boot MySQL Docker 引起:java.net.ConnectException:连接被拒绝(连接被拒绝)