0

我对安卓很陌生。我想构建客户端/服务器应用程序,其中客户端运行 android 而服务器运行 Java。

客户代码

package com.example.android;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.AsyncTask;
import java.io.*;
import java.net.*;

public class MainActivity extends Activity {

static String line= "works";
private MyTask mt;
private EditText nameField;
private TextView nameView;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    nameField =(EditText) findViewById(R.id.FirstInputField);
    nameView =(TextView) findViewById(R.id.DisplayText);
    button = (Button) findViewById(R.id.button1);   

        button.setOnClickListener(new OnClickListener() {               

            public void onClick(View v) {
                mt=new MyTask();
                mt.execute();

            }
        });
}

private class MyTask extends AsyncTask<Void, String, Void>

{
    protected void onPreExecute()
    {

    }


    @Override
    protected Void doInBackground(Void... params) {


        Socket s;
        try {
            s = new Socket ("172.17.20.42", 8888);
            ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
            ObjectInputStream ios=new ObjectInputStream(s.getInputStream());
            oos.writeObject(line);              
            oos.close();
            ios.close();    
            s.close();
        } 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.main, menu);
    return true;


}   

}

该程序运行良好,但问题是,我正在发送静态字符串。

doInBackground方法无权访问 UI 线程。问题是“如何发送在 UI 中输入的字符串?”

先感谢您

4

3 回答 3

1
private class MyTask extends AsyncTask<Void, String, Void>
{
    String line;
    public MyTask(String line) {
        this.line = line;
    }

然后在onCreate()

public void onClick(View v) {
    mt=new MyTask(nameView.getText().toString());
    mt.execute();
}

注意:当您为发送的每一行实例化一个新对象时,这不是最有效或最节省内存的方法MyTask,但这种方法要求对代码进行较少的更改,就像现在一样。

于 2013-06-07T19:38:00.603 回答
0

在单击按钮时,在构造函数或 execute() 方法中将 nameView.getText() 传递给 MyTask(将要求您将参数作为字符串接受...字符串并将其读取为字符串 [0])

于 2013-06-07T19:47:12.880 回答
0

将 MyTask 的第一个泛型参数设为 String 类型:

private class MyTask extends AsyncTask<String, String, Void>

{
    protected void onPreExecute()
    {

    }


@Override
protected Void doInBackground(String... params) {

    String stringToSend = params[0];

    Socket s;
    try {
        s = new Socket ("172.17.20.42", 8888);
        ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
        ObjectInputStream ios=new ObjectInputStream(s.getInputStream());
        oos.writeObject(stringToSend);              
        oos.close();
        ios.close();    
        s.close();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    return null;
}

并通过以下方式传递字符串参数execute

mt.execute(new String[1] {nameField.getText().toString()});
于 2013-06-07T19:47:22.087 回答