When I tried to start my app which checks something with the database I have encountered irrational error. My app threw an java.lang.RuntimeException with the line: OutputStream ops = http.getOutputStream();
My code is:
String result = "";
String connstr = "http://myip:8080/androidlogin.php";
try {
URL url = new URL(connstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
Log.d("TAG", "1");
OutputStream ops = http.getOutputStream();
Log.d("TAG", "2");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops,"UTF-8"));
Log.d("TAG", "3");
String data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(id,"UTF-8");
Log.d("TAG", "4");
writer.write(data);
writer.flush();
writer.close();
ops.close();
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips,"ISO-8859-1"));
String line ="";
while ((line = reader.readLine()) != null)
{
result += line;
}
reader.close();
ips.close();
http.disconnect();
return result;
} catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
return result;
And my error looks like this:
2020-03-13 08:01:07.460 10633-10633/com.hu.jam E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hu.jam, PID: 10633
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hu.jam/com.hu.jam.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: android.os.NetworkOnMainThreadException
The most interesting part is that between Log 1 and error there are three interesting messages on the logcat:
2020-03-13 08:01:07.442 10633-10633/com.hu.jam D/TAG: 1
2020-03-13 08:01:07.444 10633-10633/com.hu.jam I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-03-13 08:01:07.444 10633-10633/com.hu.jam I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-03-13 08:01:07.451 10633-10633/com.hu.jam D/AndroidRuntime: Shutting down VM
2020-03-13 08:01:07.460 10633-10633/com.hu.jam E/AndroidRuntime: FATAL EXCEPTION: main
Here is a php script:
<?php
$db = "database1";
$user = $_POST["user"];
$host = "localhost";
$conn = mysqli_connect($host, "root", "", $db);
if($conn){
echo "connected....!";
$q= "select * from article where a_title='$user'";
$result = mysqli_query($conn, $q);
if(mysqli_num_rows($result) > 0){
echo"login successfull...!";
}else {
echo"login failed....!";
}
}else{
echo"connection failed....!";
}
?>
What is the problem with my code? Everything seems to be alright with it, yet it does not work...