请求协助解决org.apache.http.conn.HttpHostConnectException连接拒绝问题
org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2 refused Hey there, let's work through this connection refused issue step by step. I've dealt with similar problems using LAPP stacks and Android devices before, so here are the critical checks to run:
1. Verify your local Apache server is running and reachable
Before looking at the Android side, confirm your Ubuntu server is working properly:
- Open a browser on your Ubuntu machine and navigate to
http://localhost/project/insert.php— you should get a valid response (even if it's an empty JSON or a script error, as long as it loads without a "connection refused" message). - If that fails, check Apache's status with:
If it's not running, start it with:sudo systemctl status apache2sudo systemctl start apache2
2. Fix Ubuntu firewall restrictions
Ubuntu's default ufw firewall might be blocking incoming requests to port 80:
- Temporarily disable the firewall to test:
If the connection works after this, re-enable the firewall and allow port 80 traffic permanently:sudo ufw disablesudo ufw allow 80/tcp sudo ufw enable
3. Critical Fix: Use the correct IP for your physical phone
You mentioned using a physical phone connected via USB as your test device — this is the most likely root cause:
http://10.0.2.2is only for the Android Emulator (it maps to the emulator host'slocalhost). For a physical phone, you need to use your Ubuntu machine's local network IP address.- Find your Ubuntu IP with:
Look for theip addr showinetline under your active network interface (e.g.,wlan0for WiFi,eth0for Ethernet) — it'll look like192.168.x.x. - Update your Java code's
HttpPostURL to use this IP:HttpPost httppost = new HttpPost("http://192.168.1.100/project/insert.php"); // Replace with your actual IP
4. Ensure PHP script and directory have proper permissions
Make sure Apache can access your project files:
- Set correct ownership for your project directory:
sudo chown -R www-data:www-data /var/www/html/project/ - Set file permissions:
sudo chmod -R 755 /var/www/html/project/ - Also, test your
insert.phpdirectly to rule out script errors — visit its URL in your browser and check for any PHP parse errors.
5. Confirm phone and Ubuntu are on the same network
If using WiFi, ensure both your phone and Ubuntu machine are connected to the same local network. If using USB tethering, check that Ubuntu recognizes the USB network interface and has a valid IP on that interface.
Bonus: Update your deprecated HTTP client
Note that DefaultHttpClient is deprecated in newer Android versions. For future-proofing, consider switching to HttpURLConnection or a modern library like OkHttp, but this isn't the cause of your current connection issue.
Your Original Code (for reference)
package com.example.testingdatabase; import android.os.AsyncTask; import android.util.Log; import android.widget.EditText; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class test extends AsyncTask { String name; String password; String desig; InputStream is=null; String result=null; String line=null; int code; EditText namee; EditText pass; EditText desi; @Override protected Object doInBackground(Object[] objects) { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("name", name)); nameValuePairs.add(new BasicNameValuePair("password", password)); nameValuePairs.add(new BasicNameValuePair("desig", desig)); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/project/insert.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("pass 1", "connection success "); } catch (Exception e) { Log.e("Fail 1", e.toString()); System.out.println("sorry,invalid ip"); // Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); Log.e("pass 2", "connection success "); } catch (Exception e) { Log.e("Fail 2", e.toString()); } try { JSONObject json_data = new JSONObject(result); code = (json_data.getInt("code")); if (code == 1) { System.out.println("sucess"); //Toast.makeText(getBaseContext(),"Inserted Successfully", Toast.LENGTH_SHORT).show(); } else { System.out.println("sorry"); //Toast.makeText(getBaseContext(),"Sorry, Try Again", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Log.e("Fail 3", e.toString()); } return null; } }
内容的提问来源于stack exchange,提问作者Fathima Basheer

