UrlConnection does not support output

by Hershiv Haria » Mon, 07 Dec 2009 13:55:58 GMT


Sponsored Links
 Hi all,

I'm currently trying to get an Android application to connect and send
a protocol buffer to a servlet on App Engine.
However, when I get to the line "OutputStream out =
urlc.getOutputStream()" I get the exception that it "does not support
output."

I've not found many people with this issue, and no posted fixes apart
from using newer SDKs. However, this is for a university project and
I'm sticking to 1.5, so any help would be appreciated.

The only other relevant information I can think of is I have added the
uses internet permission, again I'm on SDK1.5 and I'm running in
Eclipse.

The code is as follows:

package my.transferclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import my.tft.protos.RandomMessageProtos.Note;
import my.tft.protos.RandomMessageProtos.RandomMessage;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class TransferClient extends Activity {

        private final static String TAG = "TransferClient";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
        URL url = new URL(" http://10.0.2.2 :8080/testfileupload");
        URLConnection urlc = url.openConnection();

        OutputStream out = urlc.getOutputStream();

        RandomMessage.Builder rm = RandomMessage.newBuilder();
        Note.Builder note = Note.newBuilder();

        note.setName("Blargy McBlarg");
        note.setMessage("This is a random message");

        rm.addNote(note);

        rm.build().writeTo(out);
        out.close();

        BufferedReader rd = new BufferedReader(new InputStreamReader
(urlc
                                .getInputStream()));
                String line;
                System.out.println("Respose is:");
                while ((line = rd.readLine()) != null) {
                        System.out.println(line);
                }
                rd.close();

        } catch (Exception e) {
                Log.v(TAG, e.getMessage());
        } // catch
    }
}

--



Other Threads

1. Problem with camera preview in portrait mode

Hi all,

I'm not able to display the camera preview in portrait mode with Samsung S.
The preview is not well oriented. I have try to rotate it with the following
camera API but nothing change.
*params.set("orientation", "landscape");
params.set("rotation", 0);

*Is there a known issue on Samsung S (2.1), because I don't have this
problem with HTC Desire (2.1) ?

Is it related to:
6558 <http://code.google.com/p/android/issues/detail?id=6558>: Camera API
lacks a way to set orientation to portrait
9901 <http://code.google.com/p/android/issues/detail?id=9901>: Camera
preview only works in landscape


Cyril

____________________
*Part of source code:

*/**
 *
 */
public class ARPreviewer extends SurfaceView implements
SurfaceHolder.Callback{
    private SurfaceHolder mHolder;
    private int mpreview_width, mpreview_height; // requested frame size
    private byte[] mybuffer=null;
    ARCameraPreviewer myCameraPreviewer;


    public ARPreviewer(Context context, int preview_width, int
preview_height){
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mpreview_width = preview_width;
        mpreview_height = preview_height;

        setZOrderMediaOverlay(false);

        myCameraPreviewer = new ARCameraPreviewer(this);
    }


    public void surfaceCreated(SurfaceHolder holder) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        myCameraPreviewer.release();
    }


    public void surfaceChanged(SurfaceHolder holder, int format, int w, int
h) {

        if (mybuffer == null){ //don't restart camera when screen
orientation changed
            try {
                myCameraPreviewer.initCamera(holder, mpreview_width,
mpreview_height);
            } catch (InterruptedException e) {
                e.printStackTrace();
                return;
            }

            int bufSize = myCameraPreviewer.getBufferSize();
            mybuffer = new byte[bufSize];

            myCameraPreviewer.addCallbackBuffer(mybuffer);
            myCameraPreviewer.startPreview();
        }

        /*
        [...]
        */
    }

/**
 *
 */
public class ARCameraPreviewer implements Camera.PreviewCallback,
Camera.ErrorCallback{
    private Method mAcb;
    private Method mPCWB;
    private Camera mCamera;
    private ARPreviewer myPreviewer;
    private int pixelformat;

    ARCameraPreviewer(ARPreviewer Previewer){
        myPreviewer = Previewer;
    }

    void initCamera(SurfaceHolder holder, int preview_width, int
preview_height) throws InterruptedException{
        if(mCamera == null){
            // The Surface has been created, acquire the camera and tell it
where
            // to draw.
            int i = 0;
            while(i++ < 5){
                try{
                    mCamera = Camera.open();
                    break;
                }catch(RuntimeException e){
                    Thread.sleep(200);
                }
            }
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException exception) {
                mCamera.release();
                mCamera = null;

            }catch(RuntimeException e){
                Log.e("camera", "stacktrace", e);
            }
        }

        // Camera configuration
        if(mCamera != null){
            parameters.setPreviewSize(preview_width, preview_height);
            mCamera.setParameters(parameters);
        }

        //init setPreviewCallbackWithBuffer
        try {
            mPCWB = Class.forName("android.hardware.Camera").getMethod(
                    "setPreviewCallbackWithBuffer", PreviewCallback.class);
        } catch (Exception e) {
            Log.e("CameraView",
                    "Problem setting up for setPreviewCallbackWithBuffer: "
                            + e.toString());
        }

        //init addCallbackBuffer
        try {
            mAcb = Class.forName("android.hardware.Camera").getMethod(
                    "addCallbackBuffer", byte[].class);
        } catch (Exception e) {
            Log.e("CameraView", "Problem setting up for addCallbackBuffer:
"+ e.toString());
        }
    }


    void release(){
        if(mCamera !=null){
            mCamera.stopPreview();
            mCamera.release();
        }

        clearPreviewCallbackWithBuffer();
        mCamera = null;
        mAcb = null;
        mPCWB = null;
    }

    /**
     * Camera callback, call after a frame request
     */
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // Forward frame to previewer
        myPreviewer.onPreviewFrame(data, pixelformat);
    }


    public void addCallbackBuffer(byte[] b) {
        try {
            mAcb.invoke(mCamera, b);
        } catch (Exception e) {
            Log.e("CameraView", "invoking addCallbackBuffer failed: "+
e.toString());
        }
    }


    /**
     * Tell the camera driver to use a preview callback with buffer
     */
    private void setPreviewCallbackWithBuffer() {
        try {
            mPCWB.invoke(mCamera, this);
        } catch (Exception e) {
            Log.e("CameraView", e.toString());
        }
    }

    protected void clearPreviewCallbackWithBuffer() {
        try {
            mPCWB.invoke(mCamera, (PreviewCallback) null);
        } catch (Exception e) {
            Log.e("CameraView", e.toString());
        }
    }

    public void startPreview(){
        setPreviewCallbackWithBuffer();
        mCamera.startPreview();
    }


    /**
     * Call by preview to know the size of frame buffer
     * @return frame buffer size
     */
    public int getBufferSize(){
        // define buffer size
        PixelFormat pixelinfo = new PixelFormat();
        pixelformat = mCamera.getParameters().getPreviewFormat();
        PixelFormat.getPixelFormatInfo(pixelformat, pixelinfo);

        Size preview_size = mCamera.getParameters().getPreviewSize();

        return preview_size.width * preview_size.height *
pixelinfo.bitsPerPixel / 8;
    }
/*
    [...]
*/
}

-- 

2. Difference between StageFright and Opencore

What are the differences between StageFright and Opencore? What does
this mean to app developers?

Please point me to any links, tutorials online.

Thanks.

-- 

3. image size for live wallpaper

4. Java / Weblogic Portal Developer

5. Add my New Email ID to your Distribution List

6. Image corrupted with RFCOMM Bluetooth

7. Unable to connect to Droid 2