致命异常:AsyncTask#1 for android

时间:2014-07-16 14:08:49

标签: java android android-asynctask

首先,我正在尝试使用android进行客户端套接字和ASync任务程序。 它成功构建和安装,但是当我打开应用程序时,它会崩溃并且logcat会这样说 “E / AndroidRuntime(271):致命异常:AsyncTask#1 E / AndroidRuntime(271):java.lang.RuntimeException:执行doInBackground()时发生错误“

public class AsyTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... ad) {
        Socket socket = null;
        OutputStream os = null;
        StringBuffer output = null;
        try {
            socket = new Socket(ad[0], Integer.parseInt(ad[1]));
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }

        try {
            os = socket.getOutputStream();
            os.write("GET / \r\n".getBytes());
            os.flush();
            output = new StringBuffer("");
            InputStream is = socket.getInputStream();
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(is)));
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                output.append(line);
                output.append("\n");
            }
            bufReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();
    }

    protected void onPostExecute(String result) {
        System.out.println(result);
    }
}

0 个答案:

没有答案