我有一個按鈕點擊功能,我想轉換爲AsyncTask,因爲網絡異常。如何將按鈕點擊功能轉換爲AsyncTask android
我試過下面的代碼。有人可以指出我哪裏出錯了。
package com.icube.homeautomation;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.R.string;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class temp extends Activity {
private EditText textOut; //write msg
private TextView textIn; //show received msg
private EditText ipaddressEdt; //enter ip address of server
private EditText portno; //port no
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp_layout);
ipaddressEdt = (EditText)findViewById(R.id.EditText01);
portno=(EditText)findViewById(R.id.EditText02);
textOut = (EditText)findViewById(R.id.messageedt);
Button buttonSend = (Button)findViewById(R.id.connecttoserver);
textIn = (TextView)findViewById(R.id.TextView01);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
new DownloadImageTask().execute(ipaddressEdt.getText().toString().trim(), Integer.parseInt(portno.getText().toString().trim()));
}};
private class DownloadImageTask extends AsyncTask <Socket ,Integer, String>{
protected Socket doInBackground(String... data) {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket(data[0]);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
//if specified ip address is not found in the network
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
//close outputstream
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
//close inputsteam
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return socket;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
textIn.setText(result);
}
}
}
我收到以下錯誤
類型temp.DownloadImageTask必須實現繼承的抽象方法AsyncTask.doInBackground(插座...)
感謝,
_請有人指出我要去哪兒出錯._但是出了什麼問題?有沒有錯誤? –
保護字符串doInBackground(字符串...數據){ –