2013-03-29 50 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讓它工作 –