i'm trying to connect to xampp localhost trough android emulator using android studio. but im always getting a connection refused error.
i've read around and tried most solution such as changing localhost to 10.0.2.2 or 127.0.0.1 or 192.168.0.1 and even tried to remove the http:// or add port :8080 behind it. but still no luck.
i can run the localhost/test.php in the browser with no problem. but 10.0.2.2/test.php doesnt work.
and ofcourse i added internet permission to the manifest.xml too.
i've even tried 2 version of code but it still fails me. can anyone please help me to understand this?
here's the error shown.
04-10 02:55:15.933 2162-2178/com.example.kitd16.myapplication W/System.err﹕ java.net.ConnectException: failed to connect to /10.0.0.2 (port 80) after 60000ms: isConnected failed: ECONNREFUSED (Connection refused)
here's the code that i've tried.
URL url = new URL("http://127.0.0.1/cloudtest/test.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(30000 /* milliseconds */);
conn.setConnectTimeout(60000 /* milliseconds */);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", "" + et.getText().toString());
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
conn.setDoInput(true);
//Send request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(et.getText().toString());
wr.flush();
wr.close();
// Starts the query
conn.connect();
is = (BufferedInputStream) conn.getInputStream();
} catch (Exception ex) {
System.out.println("first error");
ex.printStackTrace();
System.exit(0);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception ex) {
}
}
}
2nd version
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog p = new ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server");
Thread thread = new Thread()
{
@Override
public void run() {
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.0.2/my_folder_inside_htdocs/connection.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(1);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("Edittext_value",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
p.dismiss();
tv.setText("Response from PHP : " + response);
}
});
}catch(Exception e){
runOnUiThread(new Runnable() {
public void run() {
p.dismiss();
}
});
System.out.println("Exception : " + e.getMessage());
}
}
};
thread.start();