简体   繁体   English

打开客户端套接字时,Android应用崩溃

[英]Android app crashing when opening Client Socket

I'm developing an android application that uses Client/Server architecture and i have an error i have a server that i built as a java application, and the client as an android application, my problem is when i click on connect button, the server displays Client Connected that means that the client socket went fine but my app crashes after that. 我正在开发使用客户端/服务器体系结构的android应用程序,但出现错误,我有一个以Java应用程序形式构建的服务器,而客户端是android应用程序,我的问题是当我单击连接按钮时,服务器显示“ Client Connected”(客户端已连接),这意味着客户端套接字运行良好,但是此后我的应用程序崩溃了。

Client Code (Android) 客户端代码(Android)

package com.example.androidpc;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Home extends Activity {

private EditText ip_txt,port_txt;
private ImageView cnt_btn;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    ip_txt = (EditText) findViewById(R.id.ip_txt);
    port_txt = (EditText) findViewById(R.id.port_txt);
    cnt_btn = (ImageView) findViewById(R.id.con_img);

    cnt_btn.setClickable(true);

    cnt_btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            new Client().execute(ip_txt.getText().toString(),port_txt.getText().toString());

        }

    });

}

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]));

            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();
        }

        return null;
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

Server (Java) 服务器(Java)

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(3333);
        System.out.println("Server is listening on port 3333");
        Socket s = ss.accept();
        System.out.println("Client Connected");
        s.close();
        ss.close();
    }

}

Android Manifest 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>

Activity XML 活动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>

Change your Client AsyncTask: 更改您的客户端AsyncTask:

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();                  
        }
    }
}

You can not use any view/UI content in doInBackground method in AsyncTask. 您不能在AsyncTask的doInBackground方法中使用任何视图/ UI内容。

Use your UI content in onProgressUpdate method 在onProgressUpdate方法中使用您的UI内容

Try this: 尝试这个:

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();
        }


     }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM