由网友(浅灰色丶邂逅)分享简介:我在使用的AsyncTask 一些麻烦,因为我从来没有遇到过它之前,没有任何线索,我在做什么吧。 基本上,我得到一个强制关闭,因为我想运行在主类的连接。可能有人有可能帮助我与添加的AsyncTask 进入code:包com.smarte.smartipcontrol;进口java.io.BufferedReader中;...
我在使用的AsyncTask
一些麻烦,因为我从来没有遇到过它之前,没有任何线索,我在做什么吧。
基本上,我得到一个强制关闭,因为我想运行在主类的连接。可能有人有可能帮助我与添加的AsyncTask
进入code:
包com.smarte.smartipcontrol;
进口java.io.BufferedReader中;
进口java.io.BufferedWriter中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口的java.io.PrintWriter;
进口的java.net.InetAddress;
进口的java.net.Socket;
进口的java.net.UnknownHostException;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
公共类IPControl延伸活动{
私人Socket套接字;
私人字符串serverIpAddress;
私有静态最终诠释REDIRECTED_SERVERPORT = 32;
公共PrintWriter的输出;
公众的BufferedReader的;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//从意图消息
意向意图= getIntent();
serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
的createConnection();
}
公共无效getModel(查看视图){
尝试 {
通过out.println([M r N);
//System.out.print("rootrn);
而(在。就绪()!);
串textStatus = readBuffer();
}赶上(IOException异常E){}
}
公共无效的createConnection(){
尝试 {
InetAddress类serverAddr = InetAddress.getByName(serverIpAddress);
插座=新的Socket(serverAddr,REDIRECTED_SERVERPORT);
}赶上(UnknownHostException异常E1){
e1.printStackTrace();
}赶上(IOException异常E1){
e1.printStackTrace();
}
尝试 {
OUT =的新PrintWriter(新的BufferedWriter(新OutputStreamWriter(socket.getOutputStream())),TRUE);在=新的BufferedReader(新的InputStreamReader(socket.getInputStream()));
而(在。就绪()!);
readBuffer();
通过out.println(根 r N);
//System.out.print("rootrn);
而(在。就绪()!);
readBuffer();
通过out.println(根 r N);
//System.out.print("rootrn);
而(在。就绪()!);
readBuffer();
}赶上(IOException异常E){}
//R.id.textStatus
}
私人字符串readBuffer()抛出IOException异常{
弦乐味精=;
而(在。就绪()){
MSG = MSG +(炭)的.read();
}
//System.out.print(msg);
如果(msg.indexOf(SNX_COM>中!)= -1)返回msg.substring(0,msg.indexOf(SNX_COM>中));
否则返回味精;
}
}
解决方案
它是如何看migtht就像刚一简单的例子:
公共类IPControl延伸活动{
私人ProgressDialog PD = NULL;
私人字符串数据= NULL;
私人Socket套接字;
私人字符串serverIpAddress;
私有静态最终诠释REDIRECTED_SERVERPORT = 32;
公共PrintWriter的输出;
公众的BufferedReader的;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
尝试{
this.pd = ProgressDialog.show(这一点,加载。,请等待......,真,假);
新AsyncAction()执行()。
}赶上(例外五){
e.printStackTrace();
}
}
私有类AsyncAction扩展的AsyncTask<字符串,太虚,字符串> {
保护无效doInBackground(字符串参数... args){
尝试 {
InetAddress类serverAddr = InetAddress.getByName(serverIpAddress);
插座=新的Socket(serverAddr,REDIRECTED_SERVERPORT);
}赶上(UnknownHostException异常E1){
e1.printStackTrace();
}赶上(IOException异常E1){
e1.printStackTrace();
}
尝试 {
OUT =的新PrintWriter(新的BufferedWriter(新OutputStreamWriter(socket.getOutputStream())),TRUE);
在=新的BufferedReader(新的InputStreamReader(socket.getInputStream()));
而(in.ready()!);
readBuffer();
通过out.println(根 r N);
//System.out.print("rootrn);
而(在。就绪()!);
readBuffer();
通过out.println(根 r N);
//System.out.print("rootrn);
而(在。就绪()!);
弦乐味精=;
而(in.ready()){
MSG = MSG +(炭)的.read();
}
}赶上(IOException异常E){}
返回null; //返回要传递给onPostExecute什么()
}
保护无效onPostExecute(字符串结果){
// resultis从doInbackground返回的数据
IPControl.this.data =结果;
如果(IPControl.this.pd!= NULL){
IPControl.this.pd.dismiss();
}
}
}
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("[mrn");
//System.out.print("rootrn");
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("rootrn");
//System.out.print("rootrn");
while (! in .ready());
readBuffer();
out.println("rootrn");
//System.out.print("rootrn");
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;
}
}
解决方案
Just one quick example of how it migtht look like:
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("rootrn");
//System.out.print("rootrn");
while (! in .ready());
readBuffer();
out.println("rootrn");
//System.out.print("rootrn");
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();
}
}
}
相关推荐
最新文章