SockettimeoutException并关闭VM

时间:2017-12-19 06:15:03

标签: java php android

我正在创建一个Android项目,其中Registrationadmin活动使用HttpURLConnection连接到PHP文件(在本地服务器,localhost中); 我在运行项目时遇到问题我正在获取此D / NetworkSecurityConfig:使用来自资源network_security_config的Network Security配置debugBuild:true 我已经给出了连接和读取超时。  Registrationadmin.java代码是:

package com.example.hp.healthcareapp;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

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

import static android.content.ContentValues.TAG;

/**
 * Created by HP on 12/18/2017.
 */

public class BackgroundTask extends AsyncTask<String,Void,String> {
    AlertDialog alertDialog;
    Context ctx;
    BackgroundTask(Context ctx)
    {
        this.ctx =ctx;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("Login Information....");
    }

    @Override
    protected String doInBackground(String... params) {
        String reg_url = "http://10.14.83.2:8080/register.php";
        String login_url = "http://192.168.2.4:8080/login.php";
        String method = params[0];
        if (method.equals("register")) {
            String admname = params[1];
            //Log.d(TAG,admname);
            String admemail = params[2];
            String admpass = params[3];
            String admmob = params[4];
            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setReadTimeout(10000);
                httpURLConnection.setConnectTimeout(15000);
                //httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("admname", "UTF-8")+"="+URLEncoder.encode(admname, "UTF-8") + "&" +
                        URLEncoder.encode("admemail", "UTF-8")+"="+URLEncoder.encode(admemail, "UTF-8") + "&" +
                        URLEncoder.encode("admpass", "UTF-8")+"="+URLEncoder.encode(admpass, "UTF-8")+"&"+
                        URLEncoder.encode("admmob", "UTF-8")+"="+URLEncoder.encode(admmob, "UTF-8");
                Log.d(TAG,data);
                httpURLConnection.connect();
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return "Registration Success...";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if(method.equals("login"))
        {

        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        if(result.equals("Registration Success..."))
        {
            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        }
        else
        {
            alertDialog.setMessage(result);
            alertDialog.show();
        }

    }
}

我的BackgroundTask.java是

<?php
echo "this is it";
require "init.php";

$admmane = (isset($_POST['admname']) ) ? $_POST['admname'] : '';
$admpass = (isset($_POST['admpass']) ) ? $_POST['admpass'] : '';
$admemail = (isset($_POST['admemail']) ) ? $_POST['admemail'] : '';
$admmob=(isset($_POST['admmob']) ) ? $_POST['admmob'] : '';

//$admmane = "sdf";
//$admpassword = "sdf";
//$admemail = "sdf@r54";
//$admmob="sdf";

$sql_query= "insert into admin values ('$admmane','$admemail','$admpass','$admmob');";
if(mysqli_query($con, $sql_query)){
    echo '{"message":"able to save the data to the database."}';
}

?>

register.php是

12-19 11:24:48.803 2555-2560/com.example.hp.healthcareapp I/zygote: Increasing code cache capacity to 1024KB
12-19 11:24:55.026 2555-3178/com.example.hp.healthcareapp D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
12-19 11:24:55.048 2555-3178/com.example.hp.healthcareapp D/ContentValues: admname=shaista&admemail=shaista8080%40gmail.com&admpass=Pro%401234&admmob=9593626313
12-19 11:24:55.277 2555-2618/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0x9f1840c0: ver 2 0 (tinfo 0x9f183300)
12-19 11:24:55.393 2555-2618/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0x9f1840c0: ver 2 0 (tinfo 0x9f183300)
12-19 11:24:55.481 2555-2618/com.example.hp.healthcareapp D/EGL_emulation: eglMakeCurrent: 0x9f1840c0: ver 2 0 (tinfo 0x9f183300)
12-19 11:24:55.495 2555-2618/com.example.hp.healthcareapp D/OpenGLRenderer: endAllActiveAnimators on 0x8bb11880 (RippleDrawable) with handle 0x9f183b50
12-19 11:25:05.057 2555-3178/com.example.hp.healthcareapp W/System.err: java.net.SocketTimeoutException: timeout
12-19 11:25:05.058 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:212)
12-19 11:25:05.059 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:261)
12-19 11:25:05.059 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
12-19 11:25:05.060 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
12-19 11:25:05.061 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
12-19 11:25:05.063 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
12-19 11:25:05.065 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
12-19 11:25:05.065 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
12-19 11:25:05.067 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
12-19 11:25:05.067 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
12-19 11:25:05.068 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
12-19 11:25:05.071 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
12-19 11:25:05.071 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
12-19 11:25:05.072 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.example.hp.healthcareapp.BackgroundTask.doInBackground(BackgroundTask.java:70)
12-19 11:25:05.072 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.example.hp.healthcareapp.BackgroundTask.doInBackground(BackgroundTask.java:25)
12-19 11:25:05.073 2555-3178/com.example.hp.healthcareapp W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
12-19 11:25:05.074 2555-3178/com.example.hp.healthcareapp W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
12-19 11:25:05.076 2555-3178/com.example.hp.healthcareapp W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
12-19 11:25:05.077 2555-3178/com.example.hp.healthcareapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
12-19 11:25:05.077 2555-3178/com.example.hp.healthcareapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
12-19 11:25:05.078 2555-3178/com.example.hp.healthcareapp W/System.err:     at java.lang.Thread.run(Thread.java:764)
12-19 11:25:05.079 2555-3178/com.example.hp.healthcareapp W/System.err: Caused by: java.net.SocketException: Socket closed
12-19 11:25:05.080 2555-3178/com.example.hp.healthcareapp W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:203)
12-19 11:25:05.080 2555-3178/com.example.hp.healthcareapp W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:139)
12-19 11:25:05.082 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
12-19 11:25:05.083 2555-3178/com.example.hp.healthcareapp W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
12-19 11:25:05.083 2555-3178/com.example.hp.healthcareapp W/System.err:     ... 18 more
12-19 11:25:05.085 2555-2555/com.example.hp.healthcareapp D/AndroidRuntime: Shutting down VM
12-19 11:25:05.089 2555-2555/com.example.hp.healthcareapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.hp.healthcareapp, PID: 2555
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                at com.example.hp.healthcareapp.BackgroundTask.onPostExecute(BackgroundTask.java:95)
                                                                                at com.example.hp.healthcareapp.BackgroundTask.onPostExecute(BackgroundTask.java:25)
                                                                                at android.os.AsyncTask.finish(AsyncTask.java:695)
                                                                                at android.os.AsyncTask.-wrap1(Unknown Source:0)
                                                                                at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我正在运行该项目的logcat是:

{{1}}

1 个答案:

答案 0 :(得分:0)

请检查以下2个API端点是否正常工作 你可以用邮递员来检查 在服务器上运行app(从下面的url放置服务器ip) String reg_url =“http://10.14.83.2:8080/register.php”; 字符串login_url =“http://192.168.2.4:8080/login.php”;

确保这些是否有效。应返回200响应。 然后,您可以检查数据库是否已保存注册数据。