Относительно ошибки HttpConnection - PullRequest
0 голосов
/ 17 февраля 2012

Я создал программу J2me для HttpConenction на http://www.google.com
Но когда я бегу, я получаю ошибку

error 10054 during TCP read 

В этом я только что создал HttpConnection и считал строку из inputtream
При этом создается объект Http-соединения, после чего вызывается входной поток, который читается из URL, т.е.

Ниже приведен код

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.OutputConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class NetworkFile extends MIDlet implements CommandListener {
    Form form;
    Display display;
    Command start,exit;

    public NetworkFile() {
        // TODO Auto-generated constructor stub
        form=new Form(null);
        exit=new Command("Exit",Command.EXIT, 0);
        start=new Command("Start",Command.OK, 1);
        form.addCommand(exit);
        form.addCommand(start);
        form.setCommandListener(this);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        display=Display.getDisplay(this);
        display.setCurrent(form);
    }

    public void commandAction(Command c, Displayable d) {
        // TODO Auto-generated method stub

        if (c==start) {

            Abc t=new Abc(this);
            t.start();


        } else if(c==exit){
            notifyDestroyed();
        }


    }

}


class Abc extends Thread
{
    NetworkFile net1;
    HttpConnection hs=null;
    DataOutputStream requestOutputStream;
    InputStream is=null;
    StringBuffer sb=null;
    Abc(NetworkFile net1)
    {
        this.net1=net1;
    }
    public void run()
    {
        try {
            /*OutputConnection connection=(OutputConnection)Connector.open("file:///D:/lamp.txt;append=true",Connector.WRITE);
            OutputStream out=connection.openOutputStream();
            PrintStream output=new PrintStream(out);
            output.println("Hi , This is a J2ME project ");
            out.close();
            connection.close();
            Alert alert=new Alert("Alert", "Data Written", null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            net1.display.setCurrent(alert);*/

            System.out.println("Line 1");
            hs = (HttpConnection)Connector.open("http://www.google.com",Connector.READ_WRITE, true);
            System.out.println("Line 2");
            hs.setRequestMethod(HttpConnection.POST);
            System.out.println("Line 3");
            is=hs.openInputStream();
            System.out.println("Line 4");
            int ch=0;

            sb = new StringBuffer();
            for (int i = 0; i < 150; i++) {
                System.out.println("Line 5");
                ch=is.read();
                System.out.println("Line 6");
                            if(ch==-1)
                                {
                                    break;
                            }
                System.out.println("ch "+ch);
                sb.append((char)ch);
            }




        } catch (ConnectionNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
            Alert alert=new Alert("Alert", " Connection ERROR !!! "+e.toString(), null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            net1.display.setCurrent(alert);
        } catch (IOException e) {
            System.out.println(e.toString());
            // TODO Auto-generated catch block
            Alert alert=new Alert("Alert", " IO  ERROR !!! "+e.toString(), null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            net1.display.setCurrent(alert);
        }catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
            Alert alert=new Alert("Alert", " Exception  ERROR !!! "+e.toString(), null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            net1.display.setCurrent(alert);
        }
        System.out.println(sb.toString());
    }
}
...