我在使用AsyncTask时遇到了一些麻烦,因为我之前从未遇到过它并且不知道我在做什么.
基本上我得到一个力量关闭,因为我试图在主类上运行连接.有人可能会帮助我在代码中添加AsyncTask:
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\r\n"); //System.out.print("root\r\n"); 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\r\n"); //System.out.print("root\r\n"); while (! in .ready()); readBuffer(); out.println("root\r\n"); //System.out.print("root\r\n"); 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; }}
解决方法:
只是一个关于migtht如何的简单示例:
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\r\n"); //System.out.print("root\r\n"); while (! in .ready()); readBuffer(); out.println("root\r\n"); //System.out.print("root\r\n"); 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(); } } }
总结 以上是内存溢出为你收集整理的java – 使用AsyncTask进行android网络连接全部内容,希望文章能够帮你解决java – 使用AsyncTask进行android网络连接所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)