我对android很新。我想构建客户端/服务器应用程序,其中客户端运行android并且Server正在运行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中键入的字符串?”
提前谢谢
答案 0 :(得分: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
对象,但这种方法对代码的更改要求比现在少。
答案 1 :(得分:0)
在点击你的按钮时,将nameView.getText()传递给MyTask,无论是在构造函数中还是在execute()方法中(将要求你接受参数为String ... string并将其读作字符串[ 0])
答案 2 :(得分:0)
创建类型为String的MyTask的第一个泛型参数:
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()});