0

So I'm trying to connect my android app to a database on an apache server hosted on wamp but I am currently having no luck. I keep getting the ol 'Connection to http://10.0.2.2 refused' error. I have the necessary permissions in the androidmanifest to connect to the internet and I've tried adding the ports 80 to the ip url but no joy. The point it keeps on crashing at is when trying to get the httpResponse.

public class task extends AsyncTask<Void, Void, Boolean>
{
    private final String mEmail;
    private final String mPassword;

    task(String email, String password) {
        mEmail = email;
        mPassword = password;
    }

    private ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
    InputStream is = null ;
    String result = "";
    protected void onPreExecute() {
        progressDialog.setMessage("Checking credentials...");
        progressDialog.show();
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                task.this.cancel(true);
            }
        });
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        try {
            // Simulate network access.
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return false;
        }

        String url_select = "http://10.0.2.2/demo.php";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            //This is where it crashes
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            //read content
            is =  httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
            //Toast.makeText(MainActivity.this, "Please Try Again", Toast.LENGTH_LONG).show();
        }
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while((line=br.readLine())!=null)
            {
                sb.append(line+"\n");
            }
            is.close();
            result=sb.toString();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error converting result "+e.toString());
        }


        for (String credential : DUMMY_CREDENTIALS) {
            String[] pieces = credential.split(":");
            if (pieces[0].equals(mEmail)) {
                // Account exists, return true if the password matches.
                return pieces[1].equals(mPassword);
            }
        }

        // TODO: register the new account here.
        return true;
    }

    protected void onPostExecute(Void v) {

        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++)
            {
                JSONObject Jasonobject = null;
                //text_1 = (TextView)findViewById(R.id.txt1);
                Jasonobject = Jarray.getJSONObject(i);

                //get an output on the screen
                //String id = Jasonobject.getString("id");
                String name = Jasonobject.getString("email");
                String password = Jasonobject.getString("password");

                if(mEmailView.getText().toString().equalsIgnoreCase(name) &&
                        mPasswordView.getText().toString().equalsIgnoreCase(password))
                {

                    this.progressDialog.dismiss();
                    String address = Jasonobject.getString("address");
                    int alarmStatus = Jasonobject.getInt("alarm_status");
                    Intent mainApp = new Intent(LoginActivity.this, MainAppActivity.class);
                    startActivity(mainApp);
                    finish();
                }
                //text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n");

            }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
}

The error log

04-25 20:24:58.339 8406-8406/project.cit.ie.hmsapp W/System: ClassLoader referenced unknown path: /data/app/project.cit.ie.hmsapp-2/lib/x86
04-25 20:24:58.821 8406-8413/project.cit.ie.hmsapp W/art: Suspending all threads took: 29.019ms
04-25 20:24:58.941 8406-8435/project.cit.ie.hmsapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-25 20:24:58.946 8406-8406/project.cit.ie.hmsapp D/: HostConnection::get() New Host Connection established 0xab4ff1c0, tid 8406
04-25 20:24:59.141 8406-8435/project.cit.ie.hmsapp I/OpenGLRenderer: Initialized EGL, version 1.4
04-25 20:24:59.342 8406-8435/project.cit.ie.hmsapp W/EGL_emulation: eglSurfaceAttrib not implemented
04-25 20:24:59.342 8406-8435/project.cit.ie.hmsapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbe9e0, error=EGL_SUCCESS
04-25 20:24:59.481 8406-8413/project.cit.ie.hmsapp W/art: Suspending all threads took: 166.545ms
04-25 20:24:59.695 8406-8406/project.cit.ie.hmsapp I/Choreographer: Skipped 41 frames!  The application may be doing too much work on its main thread.
04-25 20:25:00.437 8406-8406/project.cit.ie.hmsapp I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
04-25 20:27:35.328 8406-8435/project.cit.ie.hmsapp W/EGL_emulation: eglSurfaceAttrib not implemented
04-25 20:27:35.328 8406-8435/project.cit.ie.hmsapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbfd40, error=EGL_SUCCESS
04-25 20:27:37.486 8406-10704/project.cit.ie.hmsapp E/log_tag: Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2 refused
04-25 20:27:37.486 8406-10704/project.cit.ie.hmsapp E/log_tag: Error converting result java.lang.NullPointerException: lock == null
04-25 20:27:37.612 8406-8413/project.cit.ie.hmsapp W/art: Suspending all threads took: 13.023ms
04-25 20:27:37.835 8406-8417/project.cit.ie.hmsapp I/art: Background sticky concurrent mark sweep GC freed 18903(1023KB) AllocSpace objects, 0(0B) LOS objects, 54% free, 1740KB/3MB, paused 13.974ms total 543.099ms

Any help is appreciated, been trying to wrap my head around this for days but with no avail.

NOTE A lot of this is borrowed code and I am kind of unsure as to what it does but I do have a decent enough understanding of whats what and what its doing.

Sean
  • 131
  • 2
  • 15
  • What would happen if you call this http://10.0.2.2/demo.php from browser ? – Haresh Chaudhary Apr 25 '16 at 21:04
  • Are you using an emulator? Check [the selected answer here](http://stackoverflow.com/questions/28607160/android-emulator-connection-to-http-10-0-2-2-refused?rq=1) and see if it does not address your problem as well - essentially you set `StrictMode` before you make a call to the server. – ishmaelMakitla Apr 25 '16 at 21:29
  • @HareshChaudhary Nothing, it just times out.. – Sean Apr 26 '16 at 14:55
  • Did you change the Apache config to allow access from an ip address other than the localhost and 127.0.0.1 and ::1 addresses? Your Android device will be using an ip address I assume from you routers wifi? – RiggsFolly Apr 26 '16 at 15:49
  • @RiggsFolly I did not, how would I go about doing that? – Sean Apr 26 '16 at 17:29
  • Have a look at this http://stackoverflow.com/questions/23382627/wamp-cannot-access-on-local-network-403-forbidden/23385021#23385021 – RiggsFolly Apr 26 '16 at 19:22
  • @RiggsFolly my httpd.conf doesn't have the piece of code you said to change in the other answer, and when I add it in my server won't start up – Sean Apr 27 '16 at 19:03

0 Answers0