2011-04-13 2 views
0
private final class ServerConnectThread2 extends Thread 
{   
    private InputStreamReader _in=null; 
    private OutputStreamWriter _out=null; 
    private String _url;   
    private int _delay; 
    private String _key; 
    private String _connectionParameters; 
    OutputStream os=null; 
    FileConnection fconn=null;  

    ServerConnectThread2() 
    { 
     _key=""; 
    } 


    public void run() 
    { 
     // check internet connection available 
     setConnectionStringParameter(); 
     bulkUploaderFlag=true;  
     HttpConnection connection = null; 
     try 
     { 

      _url="http://xxx/bulkuploader.jsp"; 
      _url= EncodeURL (_url) ; 
      _url = _url + _connectionParameters;   

      URLEncodedPostData _postData = new URLEncodedPostData("",false); 
      _postData.append("totalRecord",""+totalRecord); 

      int count=0; 
       while(count < totalRecord) 
      { 


      _postData.append("accountID"+count,"C"+(String)accountIDVector.elementAt(count)); 
      _postData.append("deviceID"+count,(String)deviceIDVector.elementAt(count)); 
      _postData.append("timestamp"+count,(String)timestampVector.elementAt(count)); 
       _postData.append("statusCode"+count,(String)statusCodeVector.elementAt(count)); 
      _postData.append("latitude"+count,(String)latitudeVector.elementAt(count)); 
      _postData.append("longitude"+count,(String)longitudeVector.elementAt(count)); 
      _postData.append("gpsAge"+count,(String)gpsAgeVector.elementAt(count)); 
      _postData.append("speedKPH"+count,(String)speedKPHVector.elementAt(count)); 
      _postData.append("heading"+count,(String)headingVector.elementAt(count)); 
       _postData.append("altitude"+count,(String)altitudeVector.elementAt(count)); 
      _postData.append("transportID"+count,(String)transportIDVector.elementAt(count)); 
      //_postData.append("inputMask",""+0); 
      count++; 
      } 



      byte [] postDataBytes = _postData.getBytes(); 

       // Open the connection 
      connection = (HttpConnection)Connector.open(_url, Connector.READ_WRITE, false); 
      // Set the request method as GET 
      connection.setRequestMethod("POST"); 
      //connection.setRequestMethod(HttpConnection.POST); 
      connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0"); 
      connection.setRequestProperty("Content-Language", "en-US"); 
      connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
      //connection.setRequestProperty("Content-Type","application/octet-stream"); 
      //connection.setRequestProperty("Content-Type","multipart/form-data"); 
      String len =new String(postDataBytes); 
      System.out.println(len); 
      connection.setRequestProperty("Content-Length",""+len.length()); 
      os= connection.openOutputStream(); 
      os.write(postDataBytes); 


      exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2 ]"); 
      int rc = connection.getResponseCode(); 
      if(rc == HttpConnection.HTTP_OK) 
      { 
       _in = new InputStreamReader(connection.openInputStream()); 
       // Wait for an acknowledgment, in this case an '1' character, for 'Received'.      
       //char c = (char)_in.read(); 
       int length=0; 
       exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, http==OK ]\n"); 
         StringBuffer buf = new StringBuffer();  
       while ((length = _in.read()) != -1) { 
       char c = (char)length; 
       buf.append(c); 
       if(c=='0') 
       { 
         exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, c==0,not send in bulk unsucessfully ]\n"); 

        System.out.println("DATA not send in bulk SUCCESSFULLY sct2, c==0"); 
        break; 
       } 

       if(c=='1') 
       { 
        exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, c==1, send in bulk successfully ]\n"); 

        System.out.println("DATA send in bulk SUCCESSFULLY sct2"); 
        String fullPath = "file:///SDCard/dataLog.txt"; 
        try 
        { 
         fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE); 
         if (fconn.exists()) 
         fconn.delete(); 
        } 
        catch(Exception e){} 
        finally 
        { 
         if(fconn!=null) 
         fconn.close(); 
        } 
        break; 
       } 

      } 
     } 
     else 
     { 
      System.out.println("DATA not send in bulk SUCCESSFULLY sct2");  
      exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, http not ok ]\n"); 

     } 



      } 
      catch (Exception e) 
      { 

      exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, exception catch ]\n"); 

      exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() + ", "+e.getMessage()+" ] \n"); 
      System.out.println("DATA not send in bulk SUCCESSFULLY"+e.getMessage()); 
      } 

      finally 
      { 
       try 
       { 
        if(_in!=null) 
        _in.close(); 

        if(_out!=null) 
        _out.close(); 

        if(os!=null) 
        os.close(); 

        if(connection!=null) 
        connection.close(); 

        bulkUploaderFlag=false; 
       } 
       catch (IOException ioe) 
       { 
        // Do Nothing 
       } 
       return; 
      } 

    } 
+3

어떻게됩니까? 예외가 있습니까? 오류 메시지? 똑같은 일이 일관되게 발생합니까? –

+0

tomcat 웹 서버에서 "null"응답이 있습니다. 내 전체 코드는 시뮬레이터 (10kb 문자열 데이터를 쉽게 전송)를 위해 잘 작동하지만,이 코드를 장치 (Blackberry 9000)에서 실행하면 NULL이 발생합니다. –

답변

0

나는 "dos.flush()"메소드를 호출해야한다고 생각합니다. 내 코드가 완벽하게 실행되며 postContent는 문자열 버퍼이며 contanins 300KB 문자열입니다.

 if (getPostContent() == null) { 
       httpConnection.setRequestMethod(HttpConnection.GET); 
      } else { 
       String type = "application/x-www-form-urlencoded"; 
       byte[] bytes = postContent.toString().getBytes("UTF-8"); 
       httpConnection.setRequestProperty("Content-Type", type); 
       httpConnection.setRequestProperty("Content-Length", bytes.length + ""); 
       httpConnection.setRequestMethod(HttpConnection.POST); //setupPost method for this conn 
       DataOutputStream dos = new DataOutputStream(httpConnection.openOutputStream()); 
       dos.write(bytes); 
       dos.flush(); 
       dos.close(); 
      } 
+0

"플러시"방법을 사용했지만 여전히 동일한 오류가 있습니다. –

+0

JSP 페이지에 데이터가 있습니까? 그렇지 않으면 yor "_connectionParameters"를 확인하십시오. – y0rk

+0

변경'connection.setRequestProperty ("Content-Length", "+ postDataBytes.length());' – y0rk

관련 문제