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


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

     private Socket client;
     private PrintWriter printwriter;
     private EditText etMsg, etIP, etPort;
     private Button button;
     private String messsage;
     int port=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etIP=(EditText)findViewById(R.id.editText1);
        etPort=(EditText)findViewById(R.id.editText2);
        etMsg=(EditText)findViewById(R.id.editText3);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

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

                messsage=etMsg.getText().toString();
                etMsg.setText("");
                port=Integer.parseInt(etPort.getText().toString());

                try
                {
                    client=new Socket(etIP.getText().toString(), port);
                    printwriter= new PrintWriter(client.getOutputStream(),true);
                    printwriter.write(messsage);
                    printwriter.flush();
                    printwriter.close();
                    client.close();
                }

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



            }
        });
    }


}

以上是我的套接字编程代码。我在 2.2 froyo 中开发了它。它在 Android 设备上运行良好,并为我提供了正确的输出。但是,当我在最新版本的 Android(例如 IceCreamSandwhich 和 Jellybean)上开发和运行此代码时,它会向我显示一个错误,例如“不幸的是,应用程序已停止”。有什么问题,我不明白。请帮我解决这个问题。清单文件是

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>


    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.empty.MainActivity"
            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>
4

2 回答 2

0

您可以像这样在不同的线程中移动您的网络相关任务

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

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private Socket client;
    private PrintWriter printwriter;
    private EditText etMsg, etIP, etPort;
    private Button button;
    private String messsage;
    int port = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etIP = (EditText) findViewById(R.id.editText1);
        etPort = (EditText) findViewById(R.id.editText2);
        etMsg = (EditText) findViewById(R.id.editText3);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

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

                messsage = etMsg.getText().toString();
                etMsg.setText("");
                port = Integer.parseInt(etPort.getText().toString());

                new Thread(new Runnable() 
                {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        try 
                        {
                            client = new Socket(etIP.getText().toString(), port);
                            printwriter = new PrintWriter(client.getOutputStream(),true);
                            printwriter.write(messsage);
                            printwriter.flush();
                            printwriter.close();
                            client.close();
                        }

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


            }
        });
    }

}

如果您遇到任何问题,请告诉我。

于 2013-01-10T06:05:03.227 回答
0

您可以将此代码用于您的 mainActivity.java 文件

sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
于 2016-05-11T01:06:33.873 回答