使用 AsyncTask 进行安卓网络连接 [英] Using AsyncTask for android network connection

查看:26
本文介绍了使用 AsyncTask 进行安卓网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 AsyncTask 时遇到了一些问题,因为我以前从未遇到过它,也不知道我在用它做什么.

I am having some trouble using AsyncTask as I have never come across it before and have no clue what I am doing with it.

基本上我正在强制关闭,因为我试图在主类上运行连接.有人可以帮我将 AsyncTask 添加到代码中:

Basically I am getting a force close because I am trying to run the connection on a main class. Could someone possibly help me with adding AsyncTask into the code:

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

  private Socket socket;
  private String serverIpAddress;
  private static final int REDIRECTED_SERVERPORT = 32;
  public PrintWriter out;
  public BufferedReader in ;

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
        createConnection();

      }

  public void getModel(View view) {
    try {
      out.println("[m
");
      //System.out.print("root
");
      while (! in .ready());
      String textStatus = readBuffer();

    } catch (IOException e) {}
  }

  public void createConnection() {
    try {
      InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
      socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
    } catch (UnknownHostException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    try {
      out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      while (! in .ready());
      readBuffer();
      out.println("root
");
      //System.out.print("root
");
      while (! in .ready());
      readBuffer();
      out.println("root
");
      //System.out.print("root
");
      while (! in .ready());
      readBuffer();
    } catch (IOException e) {}


    //R.id.textStatus
  }

  private String readBuffer() throws IOException {
    String msg = "";

    while ( in .ready()) {
      msg = msg + (char) in .read();
    }
    //System.out.print(msg);
    if (msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else return msg;
  }

}

推荐答案

一个简单的例子:

public class IPControl extends Activity {
    private ProgressDialog pd = null;
    private String data = null;
    private Socket socket;
    private String serverIpAddress;
    private static final int REDIRECTED_SERVERPORT = 32;
    public PrintWriter out;
    public BufferedReader in ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        try{   
            this.pd = ProgressDialog.show(this, "Loading..", "Please Wait...", true, false);
            new AsyncAction().execute();

        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class AsyncAction extends AsyncTask<String, Void, String> {
        protected Void doInBackground(String... args) { 
            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (! in.ready());
                readBuffer();
                out.println("root
");
                //System.out.print("root
");
                while (! in .ready());
                readBuffer();
                out.println("root
");
                //System.out.print("root
");
                while (! in .ready());
                String msg = "";

                while (in.ready()) {
                    msg = msg + (char) in .read();
                }
            } catch (IOException e) {}

        return null;//returns what you want to pass to the onPostExecute()
    }

    protected void onPostExecute(String result) {
        //resultis the data returned from doInbackground
        IPControl.this.data = result;

        if (IPControl.this.pd != null) {
            IPControl.this.pd.dismiss();
        }
    } 
}

这篇关于使用 AsyncTask 进行安卓网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆