我如何使用OutputStream?

时间:2015-07-25 20:35:44

标签: java android

    private byte[] Get(String urlIn) {
        URL url = null;
        String urlStr="http://10.0.0.2:8098";
        if (urlIn!=null)
            urlStr=urlIn;
        try {
            url = new URL(urlStr);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        HttpURLConnection urlConnection = null;
        try{
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            byte[] buf=new byte[10*1024];
            int szRead = in.read(buf);
            byte[] bufOut;
            OutputStream outstream = new OutputStream(urlConnection.getOutputStream());
            PrintWriter out = new PrintWriter(outstream);
            out.write("Hello Server!");
            if (szRead==10*1024) {
                throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
            }
            else {
                bufOut = Arrays.copyOf(buf, szRead);
            }
            return bufOut;
        }
        catch (IOException e){
            e.printStackTrace();
            return null;
        }
        finally{
            if (urlConnection!=null)
                urlConnection.disconnect();
        }
    }

首先,我不确定是否在正确的位置添加了OutputStream。

第二,我在线上收到错误:

OutputStream outstream = new OutputStream(urlConnection.getOutputStream());

OutputStream是抽象无法实例化的

我还在onTouchEvent中添加了一个线程:

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float eventX = event.getX();
            float eventY = event.getY();
            float lastdownx = 0;
            float lastdowny = 0;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    path.moveTo(eventX, eventY);
                    circlePath.addCircle(eventX, eventY, 50, Path.Direction.CW);
                    lastdownx = eventX;
                    lastdowny = eventY;
                    Thread t = new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            byte[] response = Get(null);
                            if (response!=null)
                                Logger.getLogger("MainActivity(inside thread)").info(response.toString());   
                        }
                    });
                    t.start();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    path.lineTo(eventX, eventY);
                    break;
                case MotionEvent.ACTION_UP:
                    // nothing to do
                    circlePath.reset();
                    break;
                default:
                    return false;
            }
            // Schedules a repaint.
            invalidate();
            return true;
        }

我想要做的是发送字符串" Hello Server!"使用http://10.0.0.2:8098

处的服务器参数

1 个答案:

答案 0 :(得分:1)

  

首先我不确定我是否在正确的位置添加了OutputStream。

你没有。您必须在读取输入之前发送输出。

  

其次我收到了错误:

OutputStream outstream = new OutputStream(urlConnection.getOutputStream());

应该是

OutputStream outstream = urlConnection.getOutputStream();