0

I am working on a project to read mysql data into android studio using a php file. I tried running my code on an emulator to see what is happening, but the emulator simply goes to the phone's homescreen. I suspect this is caused by a runtime error, but im not entirely sure. Does anyone know what could be causing this?

Here is the code to my MainActivity.java file for reference

package com.example.myapplication;
import android.net.http.Connection;
import android.os.AsyncTask;
//didnt get other one
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
//import java.sql.Connection;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Connector().execute();
    }

    class Connector extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            String result ="";

            String host = "http://localhost/android_connect/establishconnection.php";
            try{
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(host));
                HttpResponse response = client.execute(request);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                StringBuffer stringBuffer = new StringBuffer("");

                String line = ""; //
                while ((line = reader.readLine()) != null){
                    stringBuffer.append(line);
                    break;
                }
                reader.close();
                result = stringBuffer.toString();

            }
            catch (Exception e){
                return new String("Exception" + e.getMessage());
            }

            return result;
        }

        @Override

        protected void onPostExecute(String result) {
            Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT).show();
        }

    }



}

I expected the emulator to display the app that I am trying to implement. I dont think the code is currently complete, but I wanted to see what would be output for reference. EDIT: I found an error in the logcat that seems to relate to the issue that I am having. does anyone understand what exactly is causing this? it seems to be something related to my apache server, but im not entirely sure error

  • Did you check the LogCat? You find it on the bottom line of Android Studio. And beware: there are circumstances where a Toast is not displayed on an Emulator. Better to use Log.d instead of Toast and check the LogCat. – Mario Huizinga Apr 02 '22 at 15:47
  • I don’t know how routing with an emulator works, but I wouldn’t be surprised if `localhost` in the emulator isn’t what you expect it to be, unless you are running a PHP server inside of the emulator, too? – Chris Haas Apr 02 '22 at 15:54
  • To debug this, don’t run it in the background or async, and remove the try/catch. You should see a very loud and more obvious error message. – Chris Haas Apr 02 '22 at 15:55

0 Answers0