gpt4 book ai didi

java - 输入流 = httpURLConnection.getInputStream();停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:14 24 4
gpt4 key购买 nike

我正在创建一个 Android 项目,其中 Registrationadmin Activity 使用 HttpURLConnection 连接到 PHP 文件(在本地服务器中,本地主机);

我的以下代码行有问题:输入流 = httpURLConnection.getInputStream();当我运行该项目时,应用程序在此时停止,并且它还给出 D/NetworkSecurityConfig: No Network Security Config specified, using platform default 此错误

问题出在哪里?

我在这里报告代码部分:

    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.";
}
}
}}

register.php 是

<?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."}';
}
?>

init.php 是

<?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."}';
}

?>

这是我的日志猫

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

谢谢

最佳答案

D/NetworkSecurityConfig:指示警报或警告而不是错误,但应用程序仍然崩溃

检查互联网权限检查您的服务器协议(protocol) http 或 https并引用https://developer.android.com/training/articles/security-config.html

关于java - 输入流 = httpURLConnection.getInputStream();停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47826940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com