2012-03-07 5 views
2

POST 데이터를 보낸 후 URL에서 HTML 응답을 읽어야합니다. 이미 다음 두 가지 기능이 있지만 POST 데이터를 보내고 응답을받을 수 있도록 결합하는 방법을 모르겠습니다.BlackBerry를 통해 POST 데이터를 보낸 후 HTML 응답 받기

void postViaHttpConnection(String url) throws IOException { 
    HttpConnection c = null; 
    InputStream is = null; 
    OutputStream os = null; 
    int rc; 

    try { 
     c = (HttpConnection) Connector.open(url); 

     // Set the request method and headers 
     c.setRequestMethod(HttpConnection.POST); 
     c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT"); 
     c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); 
     c.setRequestProperty("Content-Language", "en-US"); 

     // Getting the output stream may flush the headers 
     os = c.openOutputStream(); 
     os.write("LIST games\n".getBytes()); 
     os.flush(); // Optional, getResponseCode will flush 

     // Getting the response code will open the connection, 
     // send the request, and read the HTTP response headers. 
     // The headers are stored until requested. 
     rc = c.getResponseCode(); 
     if (rc != HttpConnection.HTTP_OK) { 
      throw new IOException("HTTP response code: " + rc); 
     } 

     is = c.openInputStream(); 

     // Get the ContentType 
     String type = c.getType(); 
     processType(type); 

     // Get the length and process the data 
     int len = (int) c.getLength(); 
     if (len > 0) { 
      int actual = 0; 
      int bytesread = 0; 
      byte[] data = new byte[len]; 
      while ((bytesread != len) && (actual != -1)) { 
       actual = is.read(data, bytesread, len - bytesread); 
       bytesread += actual; 
      } 
      process(data); 
     } else { 
      int ch; 
      while ((ch = is.read()) != -1) { 
       process((byte) ch); 
      } 
     } 
    } catch (ClassCastException e) { 
     throw new IllegalArgumentException("Not an HTTP URL"); 
    } finally { 
     if (is != null) 
      is.close(); 
     if (os != null) 
      os.close(); 
     if (c != null) 
      c.close(); 
    } 
} 

그래서 나는 내가 얻을 수 있도록 그들을 결합하는 방법을 알고 있어야합니다

public static String getDataFromUrl(String url) throws IOException { 
    System.out.println("------------FULL URL = " + url + " ------------"); 
    StringBuffer b = new StringBuffer(); 
    InputStream is = null; 
    HttpConnection c = null; 

    long len = 0; 
    int ch = 0; 
    c = (HttpConnection) Connector.open(url); 
    is = c.openInputStream(); 
    len = c.getLength(); 
    if (len != -1) { 
     // Read exactly Content-Length bytes 
     for (int i = 0; i < len; i++) 
      if ((ch = is.read()) != -1) { 
       b.append((char) ch); 
      } 
    } else { 
     // Read until the connection is closed. 
     while ((ch = is.read()) != -1) { 
      len = is.available(); 
      b.append((char) ch); 
     } 
    } 

    is.close(); 
    c.close(); 
    return b.toString(); 
} 

이 기능은 URL에 POST 데이터를 전송합니다

이 함수는 표준 HTML 응답을 얻는다 POST 데이터를 전송 한 후 응답.

+1

(긴급!) 내가 모르는 새로운 기술인가요? 솔직히 말해서, 설명의 내용은 전혀 도움이되지 않습니다. 긴급한 도움을 원하면 전문가의 도움을 받아야합니다. – rwilliams

+0

죄송합니다. 내 바보 같았습니다. 프로그램에서이 문제를 간과 한 것이므로 곧 해결할 방법을 찾아야합니다. – Chro

답변

1

이 다음 코드를 시도하고 날 개미 문제

참고 알려 : URL 확장이 중요하다 (내가 와이파이 만 전을 위해 사용하고 여기에 "; 인터페이스 = 무선 랜을")

import java.io.InputStream; 
import java.io.OutputStream; 

import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 

import net.rim.blackberry.api.browser.URLEncodedPostData; 
import net.rim.device.api.io.http.HttpProtocolConstants; 

public class HttpPostSample { 
    HttpConnection hc = null; 
    StringBuffer stringBuffer = new StringBuffer(); 
    InputStream inputStream; 

    public HttpPostSample(String url) { 
     try{ 
      hc = (HttpConnection)Connector.open(url+";interface=wifi");//you have to use connection extension ";interface=wifi" this is only for wifi 
      URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false); 
      //These are your appending values and tags 
      oPostData.append("property_id","value"); 
      oPostData.append("property_name","value"); 
      oPostData.append("category","value"); 
      oPostData.append("address","value"); 
      oPostData.append("country","value"); 
      hc.setRequestMethod(HttpConnection.POST); 
      hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType()); 
      byte [] postBytes = oPostData.getBytes(); 
      hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length)); 
      OutputStream strmOut = hc.openOutputStream(); 
      strmOut.write(postBytes); 
      strmOut.flush(); 
      strmOut.close(); 

      String returnMessage = hc.getResponseMessage(); 
      System.out.println("============"+returnMessage); 
      if(hc.getResponseCode()==HttpConnection.HTTP_OK) 
      { 
       inputStream = hc.openInputStream(); 
       int c; 
       while((c=inputStream.read())!=-1) 
       { 
        stringBuffer.append((char)c); 
       } 
       System.out.println(">>>>>>>>>>>>>>>>>"+stringBuffer.toString()); 
       parseResults(stringBuffer.toString()); 

      }else{ 
       parseResults("ERROR"); 
      } 


     }catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 
    private void parseResults(String response) 
    { 
     if(response.equalsIgnoreCase("ERROR")) 
     { 
      System.out.println("Error in Connection please check your internet and Connection extension"); 
     }else{ 
      System.out.println(response); 
     } 
    } 

} 

을 당신은 당신이

콘솔 parseResults 방법으로 출력을 볼 수 있습니다

HttpPostSample post=new HttpPostSample(url); 

로 기능 이상으로 호출 할 수 있습니다

관련 문제