2013-03-29 27 views
-1

我有一个按钮点击功能,我想转换为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(插座...)

感谢,

+1

_请有人指出我要去哪儿出错._但是出了什么问题?有没有错误? –

+0

保护字符串doInBackground(字符串...数据){ –

回答

0

问题是在这里:

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

因为你不重写doIn背景与正确的返回类型,你有作为泛型类型参数传递给AsyncTask。充电doInBackground返回类型为字符串,而不是套接字:

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

    return "any string"; 
} 
+0

我做了你所说的,但按钮单击不起作用。我没有得到任何结果。基本上我想做套接字连接 – asifa

+0

@asifa:你也需要移动'textIn.setText(dataInputStream.readUTF());'在onPostExecute内部的行因为你试图从doInBackground更新textview –

+0

@asifa :: change DownloadImageTask as http ://pastebin.com/q9018nz5让它工作 –