InputStream is = httpURLConnection.getInputStream(); stop working

梦想的初衷 提交于 2019-12-24 10:18:13

问题


I'm creating an Android project where the Registrationadmin activity connects to a PHP file (in a local server, localhost) using HttpURLConnection;

I have a problem with the following code line: InputStream is = httpURLConnection.getInputStream(); When I run the project, the application stops at this point,and it also gives D/NetworkSecurityConfig: No Network Security Config specified, using platform default this error

Where is the problem?

I report the code part here:

    package com.example.hp.healthcareapp;

    import android.content.Context;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.util.Patterns;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.basgeekball.awesomevalidation.AwesomeValidation;
    import com.basgeekball.awesomevalidation.ValidationStyle;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;

    public class Registrationadmin extends AppCompatActivity {
        private EditText Nameadm, Emailadm, Passadm, Mobadm;
        private Button Registeradm;
        private AwesomeValidation awesomeValidation;
        String Name, Email, Password, Mob;
        Context ctx = this;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_registrationadmin);
            awesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
            Nameadm = (EditText) findViewById(R.id.etnameadm);
            Emailadm = (EditText) findViewById(R.id.etemailadm);
            Passadm = (EditText) findViewById(R.id.etpassadm);
            Mobadm = (EditText) findViewById(R.id.etmobadm);
            Registeradm = (Button) findViewById(R.id.btnregisteradm);
            awesomeValidation.addValidation(this, R.id.etnameadm, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", R.string.nameerror);
            awesomeValidation.addValidation(this, R.id.etemailadm, Patterns.EMAIL_ADDRESS, R.string.emailerror);
            // awesomeValidation.addValidation(this, R.id.etpasspat, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", R.string.passworderror);
            awesomeValidation.addValidation(this, R.id.etmobadm, "^[+]??[0-9]{10,13}$", R.string.mobileerror);
            //awesomeValidation.addValidation(this, R.id.etmobpat, RegexTemplate.TELEPHONE, R.string.mobileerror);
            //awesomeValidation.addValidation(this, R.id.etaddadm, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", R.string.adderror);
            String regexPassword = "(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d])(?=.*[~`!@#\\$%\\^&\\*\\(\\)\\-_\\+=\\{\\}\\[\\]\\|\\;:\"<>,./\\?]).{8,}";
            awesomeValidation.addValidation(this, R.id.etpassadm, regexPassword, R.string.passworderror);
        }

        public void btnregisteradm(View v) {
            if (awesomeValidation.validate()) {
                Name = Nameadm.getText().toString();
                Password = Passadm.getText().toString();
                Email = Emailadm.getText().toString();
                Mob = Mobadm.getText().toString();
                //Toast.makeText(ctx, "we are here", Toast.LENGTH_SHORT).show();
                BackGround b = new BackGround();
                //Toast.makeText(ctx, "we are here", Toast.LENGTH_SHORT).show();
                b.execute(Name, Password, Email, Mob);
                //Toast.makeText(ctx, "we are here", Toast.LENGTH_SHORT).show();
            }
        }

        static class BackGround extends AsyncTask<String, String, String> {
            @Override
            protected String doInBackground(String... params) {
                String admname = params[0];
                String admpassword = params[1];
                String admemail = params[2];
                String admmob = params[3];
                String data = "";
                int tmp;
                try {
                    URL url = new URL("http://10.14.83.2:8080/register.php");
                    String urlParams = "admname=" + admname + "&admemail=" + admemail + "&admpassword=" + admpassword + "&admmob=" + admmob;
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setDoOutput(true);
                    OutputStream os = httpURLConnection.getOutputStream();
                    os.write(urlParams.getBytes());
                    os.flush();
                    os.close();
                    Log.d("executes", "this is executed");
                    InputStream is = httpURLConnection.getInputStream();
                    Log.d("problem", "this line is not executed");
                    while ((tmp = is.read()) != -1) {
                        data += (char) tmp;
                    }
                    Log.d("problem8", "why is this error");
                    is.close();
                    httpURLConnection.disconnect();

                    return data;
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                    Log.d("IOEX1", e1.getMessage());
                    return "Exception: " + e1.getMessage();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("IOEX3", e.getMessage());
                    return "Exception: " + e.getMessage();
                }
            }

            @Override
            protected void onPostExecute(String s) {
                if(s.equals("")){
                    s="Data saved successfully.";
            }
    }
}}

the register.php is

<?php
echo "this is it";
error_reporting(0);
require "init.php";
$admname = $_POST["admname"];
$admpass = $_POST["admpass"];
$admemail = $_POST["admemail"];
$admmob=$_POST["admmob"];
//$admname = "sdf";
//$admpassword = "sdf";
//$admemail = "sdf@r54";
//$admmob="sdf";
$sql = "INSERT INTO `admin` (`admname`,`admemail`, `admpass`, `admmob`) 
VALUES ('".$admname."','".$admemail."' '".$admpass."', '".$admmob."');";
if(!mysqli_query($con, $sql)){
    echo '{"message":"Unable to save the data to the database."}';
}
?>

and the init.php is

<?php
echo "this is init";
error_reporting(0);

$db_name = "mydata";
$mysql_user = "root";
$mysql_pass = "";
$server_name = "localhost";

$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);

if(!$con){
    echo '{"message":"Unable to connect to the database."}';
}

?>

This is my logcat

12-17 10:46:51.950 2559-2559/? I/zygote: Not late-enabling -Xcheck:jni (already on)
12-17 10:46:51.973 2559-2559/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
12-17 10:46:52.373 2559-2559/com.example.hp.healthcareapp I/InstantRun: starting instant run server: is main process

                                                                        [ 12-17 10:46:52.399  1636: 1809 D/         ]
                                                                        HostConnection::get() New Host Connection established 0x9078a080, tid 1809


                                                                        [ 12-17 10:46:52.400  1636: 1809 W/         ]
                                                                        Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 
12-17 10:46:52.926 2559-2609/com.example.hp.healthcareapp D/OpenGLRenderer: HWUI GL Pipeline
12-17 10:46:53.046 2559-2609/com.example.hp.healthcareapp I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
12-17 10:46:53.047 2559-2609/com.example.hp.healthcareapp I/OpenGLRenderer: Initialized EGL, version 1.4
12-17 10:46:53.047 2559-2609/com.example.hp.healthcareapp D/OpenGLRenderer: Swap behavior 1
12-17 10:46:53.047 2559-2609/com.example.hp.healthcareapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-17 10:46:53.047 2559-2609/com.example.hp.healthcareapp D/OpenGLRenderer: Swap behavior 0
12-17 10:46:53.056 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglCreateContext: 0xa4b850c0: maj 2 min 0 rcv 2
12-17 10:46:53.129 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:46:53.264 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:46:53.576 2559-2559/com.example.hp.healthcareapp V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741835
12-17 10:46:53.583 2559-2559/com.example.hp.healthcareapp I/AssistStructure: Flattened final assist data: 3512 bytes, containing 1 windows, 13 views
12-17 10:47:18.106 2559-2564/com.example.hp.healthcareapp I/zygote: Do partial code cache collection, code=23KB, data=30KB
12-17 10:47:18.108 2559-2564/com.example.hp.healthcareapp I/zygote: After code cache collection, code=23KB, data=30KB
12-17 10:47:18.108 2559-2564/com.example.hp.healthcareapp I/zygote: Increasing code cache capacity to 128KB
12-17 10:47:34.711 2559-2564/com.example.hp.healthcareapp I/zygote: Do partial code cache collection, code=61KB, data=59KB
12-17 10:47:34.713 2559-2564/com.example.hp.healthcareapp I/zygote: After code cache collection, code=61KB, data=59KB
12-17 10:47:34.713 2559-2564/com.example.hp.healthcareapp I/zygote: Increasing code cache capacity to 256KB
12-17 10:48:16.020 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:16.073 2559-2564/com.example.hp.healthcareapp I/zygote: Do full code cache collection, code=101KB, data=112KB
12-17 10:48:16.073 2559-2564/com.example.hp.healthcareapp I/zygote: After code cache collection, code=96KB, data=89KB
12-17 10:48:18.492 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:18.552 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:18.690 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:18.769 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:18.814 2559-2559/com.example.hp.healthcareapp V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741834
12-17 10:48:18.822 2559-2559/com.example.hp.healthcareapp I/AssistStructure: Flattened final assist data: 3440 bytes, containing 1 windows, 12 views
12-17 10:48:18.837 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:19.106 2559-2609/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0xa4b850c0: ver 2 0 (tinfo 0xa4b83280)
12-17 10:48:21.823 2559-2564/com.example.hp.healthcareapp I/zygote: Do partial code cache collection, code=122KB, data=111KB
12-17 10:48:21.823 2559-2564/com.example.hp.healthcareapp I/zygote: After code cache collection, code=122KB, data=111KB
12-17 10:48:21.823 2559-2564/com.example.hp.healthcareapp I/zygote: Increasing code cache capacity to 512KB
12-17 10:48:27.463 2559-2564/com.example.hp.healthcareapp I/zygote: Do full code cache collection, code=243KB, data=197KB
12-17 10:48:27.464 2559-2564/com.example.hp.healthcareapp I/zygote: After code cache collection, code=238KB, data=161KB
12-17 10:48:39.837 2559-2564/com.example.hp.healthcareapp I/zygote: Do partial code cache collection, code=247KB, data=176KB
12-17 10:48:39.838 2559-2564/com.example.hp.healthcareapp I/zygote: After code cache collection, code=247KB, data=176KB
12-17 10:48:39.842 2559-2564/com.example.hp.healthcareapp I/zygote: Increasing code cache capacity to 1024KB
12-17 10:48:51.981 2559-3513/com.example.hp.healthcareapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-17 10:48:52.010 2559-3513/com.example.hp.healthcareapp D/executes: this is executed
12-17 10:49:22.165 2559-3513/com.example.hp.healthcareapp W/System.err: java.io.FileNotFoundException: http://10.14.83.2:8080/register.php
12-17 10:49:22.165 2559-3513/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
12-17 10:49:22.165 2559-3513/com.example.hp.healthcareapp W/System.err:     at com.example.hp.healthcareapp.Registrationadmin$BackGround.doInBackground(Registrationadmin.java:91)
12-17 10:49:22.165 2559-3513/com.example.hp.healthcareapp W/System.err:     at com.example.hp.healthcareapp.Registrationadmin$BackGround.doInBackground(Registrationadmin.java:70)
12-17 10:49:22.166 2559-3513/com.example.hp.healthcareapp W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
12-17 10:49:22.166 2559-3513/com.example.hp.healthcareapp W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
12-17 10:49:22.166 2559-3513/com.example.hp.healthcareapp W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
12-17 10:49:22.166 2559-3513/com.example.hp.healthcareapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
12-17 10:49:22.167 2559-3513/com.example.hp.healthcareapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
12-17 10:49:22.167 2559-3513/com.example.hp.healthcareapp W/System.err:     at java.lang.Thread.run(Thread.java:764)
12-17 10:49:22.167 2559-3513/com.example.hp.healthcareapp D/IOEX3: http://10.14.83.2:8080/register.php

Thanks


回答1:


D/NetworkSecurityConfig: indicate a alert or warning not an error but still you app crashes

check ineternet permission check your server protocol http or https and refere https://developer.android.com/training/articles/security-config.html




回答2:


you can try

   HttpURLConnection con = Connector.connect(urlAddress);
        if (con == null) {
            return null;
        }

        try {
            InputStream is = new BufferedInputStream(con.getInputStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(is));


            }

            br.close();
            is.close();


        } catch (IOException e) {
            e.printStackTrace();
        }


来源:https://stackoverflow.com/questions/47826940/inputstream-is-httpurlconnection-getinputstream-stop-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!