打开Client Socket时Android应用程序崩溃

时间:2015-04-30 17:00:49

标签: java android sockets client server

我正在开发一个使用客户端/服务器架构的Android应用程序,我有一个错误我有一个服务器,我构建为一个Java应用程序,客户端作为一个Android应用程序,我的问题是当我点击连接按钮,服务器显示Client Connected,表示客户端套接字正常,但我的应用程序在此之后崩溃。

客户端代码(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);
}
}

服务器(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

<?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>

2 个答案:

答案 0 :(得分:0)

更改您的客户端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();                  
        }
    }
}

答案 1 :(得分:0)

您不能在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();
        }


     }

}