Java - httpconection.

 
Vista:

httpconection.

Publicado por Juan Jose (1 intervención) el 09/08/2006 00:00:11
Que mas, estoy bloqueado en mi proyecto de grado y no se donde esta el daño en el archivo ConexionHttp.java que me hace la conexion a la base de datos y con un error de hilos,

si alguien sabe como arreglar se lo agradeceria.Aqui esta el codigo con el cual estoy trabajando:

package conexion;

/*
* Connection.java
*
*/

import javax.microedition.io.*;
import java.io.*;

public class ConexionHttp extends Thread{

private HttpConnection http;
private InputStream iStrm;
private OutputStream oStrm;
private String errorMsg;
private String Msg;
private String url;
private String tipo = "";
String retorno = "";
String respuesta = "";
String res = "";

//constructor
public ConexionHttp() {

// this.fijarUrl("http://localhost:8080/servidor/inicio.php?cedula=000&paceinte=0111");
this.fijarUrl("http://localhost:8080/servidor/inicio");
this.fijarTipo("post");
}

private void fijarUrl(String s) {
this.url = s;
}

private void fijarTipo(String t) {
if (t.equals("post")) {
this.tipo = "POST";
}
else if (t.equals("get")) {
this.tipo = "GET";

}
//System.out.println("tipo : " + this.tipo);
}

public void escribir(String s) {
try {
if (this.tipo.equals("GET")) {
this.escribirGet(s);
}
else if (this.tipo.equals("POST")) {
this.escribirPost(s);
}
}
catch (Exception e) {

}
}


private void escribirGet(String s) throws IOException {
url = url + "?" + s;
try {
http = (HttpConnection) Connector.open(url);
//System.out.println("gg "+http);
http.setRequestMethod(HttpConnection.GET);
iStrm = http.openInputStream();
// System.out.println("gg "+ iStrm);
}
catch (Exception e) {

}
}

/*--------------------------------------------------
* Access servlet using POST
*-------------------------------------------------*/
private void escribirPost(String s) throws IOException {

HttpConnection http = null;
OutputStream oStrm = null;
InputStream iStrm = null;
String ret = null;



try {
http = (HttpConnection) Connector.open(url);
System.out.println("http "+http);
oStrm = http.openOutputStream();

http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
http.setRequestProperty("Connection", "close");
http.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
byte data[] = s.getBytes();
oStrm.write(data);

iStrm = http.openInputStream();
ret = processServerResponse(http, iStrm);
ret=null;

}

catch (Exception e) {
}

}

public String leer() {
try {
retorno = processServerResponse(http, iStrm);
if (iStrm != null)
iStrm.close();
if (oStrm != null)
oStrm.close();
if (http != null)
http.close();
}
catch (Exception e) {

}
return respuesta;
}

public void limpiar(){
respuesta="";
}
/*--------------------------------------------------
* Process a response from a server
*-------------------------------------------------*/
public String processServerResponse(HttpConnection http, InputStream inst)
throws IOException{
if (http.getResponseCode() == HttpConnection.HTTP_OK){
System.out.println("OK");
int lon = (int) http.getLength();
if (lon != -1){
byte datos[] = new byte[lon];
inst.read(datos,0,datos.length);
respuesta = new String(datos);
System.out.println("OK "+respuesta);
}
else{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch;
while ((ch = inst.read()) != -1)
baos.write(ch);
respuesta = new String(baos.toByteArray());
System.out.println("OK "+respuesta);
baos.close();
}
}
else{
System.out.println(http.getResponseMessage());
}
return respuesta;
}

public String getErrorMessage() {
return errorMsg;
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder