Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

java - Using AsyncTask for android network connection

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.

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;
  }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Go through this simple tutorial to get a good grounding on what an AsyncTask is and how to use it:

https://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

Then try modifying your code to use async tasks. If you get problems, Just come back here :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...