-1

我有一个按钮单击功能,由于网络异常,我想将其转换为 AsyncTask。

我已经尝试了以下代码。有人可以指出我哪里出错了。

    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(Socket...)

谢谢,

4

1 回答 1

0

问题在这里:

protected Socket doInBackground(String... data) { //<<<
           ^^^^
     //......
}

因为您没有使用正确的返回类型覆盖 doInBackground ,您已将其作为泛型类型参数传递给 AsyncTask 。将 doInBackground 返回类型充电到 String 而不是 Socket :

@Override 
protected String doInBackground(String... data) { //<<<

   return "any string";
}
于 2013-03-29T07:30:35.067 回答