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