Java 打开客户端套接字时Android应用程序崩溃

Java 打开客户端套接字时Android应用程序崩溃,java,android,sockets,client,server,Java,Android,Sockets,Client,Server,我正在开发一个使用客户机/服务器架构的android应用程序,我有一个错误我有一个作为java应用程序构建的服务器,而客户机作为android应用程序,我的问题是当我点击connect按钮时,服务器显示客户端已连接,这意味着客户端套接字运行正常,但我的应用程序在此之后崩溃 客户端代码(Android) 安卓清单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.andro

我正在开发一个使用客户机/服务器架构的android应用程序,我有一个错误我有一个作为java应用程序构建的服务器,而客户机作为android应用程序,我的问题是当我点击connect按钮时,服务器显示客户端已连接,这意味着客户端套接字运行正常,但我的应用程序在此之后崩溃

客户端代码(Android)

安卓清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidpc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Home"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

活动XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2A095C"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidpc.Home" >

    <ImageView
        android:id="@+id/logo_img"
        android:contentDescription="imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:src="@drawable/applogo" />

    <EditText
        android:id="@+id/ip_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="Server IP Address" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/port_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ip_txt"
        android:layout_below="@+id/ip_txt"
        android:ems="10"
        android:hint="Port" />

    <ImageView
        android:id="@+id/con_img"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_below="@+id/port_txt"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:src="@drawable/notconnected" />

</RelativeLayout>

更改客户端任务:

private class Client extends AsyncTask<String,Void,String>{

    Socket s;
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        try {
            s = new Socket(arg0[0],Integer.parseInt(arg0[1]));
            s.close();
            return "Connection Established"
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
    @Override
    protected void onPostExecute(String result){
        if (result != null) {
            Toast.makeText(ctx,result , Toast.LENGTH_SHORT).show();          
        } else {
            Toast.makeText(ctx,"Error" , Toast.LENGTH_SHORT).show();                  
        }
    }
}
私有类客户端扩展异步任务{
插座;
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
试一试{
s=新套接字(arg0[0],Integer.parseInt(arg0[1]);
s、 close();
返回“已建立连接”
}捕获(数字格式){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
如果(结果!=null){
Toast.makeText(ctx,result,Toast.LENGTH_SHORT).show();
}否则{
Toast.makeText(ctx,“Error”,Toast.LENGTH_SHORT).show();
}
}
}

您不能在AsyncTask的doInBackground方法中使用任何视图/UI内容

在onProgressUpdate方法中使用您的UI内容

试试这个:

private class Client extends AsyncTask<Void,Void,Void>{

    Socket s;
    @Override
    protected Void doInBackground(String... arg0) {
        // TODO Auto-generated method stub
         publishProgress();
    }


protected void onProgressUpdate() {

        try {
            s = new Socket(arg0[0],Integer.parseInt(arg0[1]));

            Toast.makeText(ctx, "Connection Established", Toast.LENGTH_SHORT).show();
            s.close();

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


     }

}
私有类客户端扩展异步任务{
插座;
@凌驾
受保护的Void doInBackground(字符串…arg0){
//TODO自动生成的方法存根
出版进度();
}
受保护的void onProgressUpdate(){
试一试{
s=新套接字(arg0[0],Integer.parseInt(arg0[1]);
Toast.makeText(ctx,“已建立连接”,Toast.LENGTH_SHORT.show();
s、 close();
}捕获(数字格式){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}

Put
Toast.makeText(ctx,“已建立连接”,Toast.LENGTH\u SHORT).show()内部
onPostExecute
和是从
doInBackground
中删除
Toast
是对UI线程的更新,不能在
doInBackground
中完成。对于来自
AsyncTask的UI更新,提供了onPostExecute
result@JibranKhan它成功了,非常感谢你。
private class Client extends AsyncTask<String,Void,String>{

    Socket s;
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        try {
            s = new Socket(arg0[0],Integer.parseInt(arg0[1]));
            s.close();
            return "Connection Established"
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
    @Override
    protected void onPostExecute(String result){
        if (result != null) {
            Toast.makeText(ctx,result , Toast.LENGTH_SHORT).show();          
        } else {
            Toast.makeText(ctx,"Error" , Toast.LENGTH_SHORT).show();                  
        }
    }
}
private class Client extends AsyncTask<Void,Void,Void>{

    Socket s;
    @Override
    protected Void doInBackground(String... arg0) {
        // TODO Auto-generated method stub
         publishProgress();
    }


protected void onProgressUpdate() {

        try {
            s = new Socket(arg0[0],Integer.parseInt(arg0[1]));

            Toast.makeText(ctx, "Connection Established", Toast.LENGTH_SHORT).show();
            s.close();

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


     }

}