2012-12-14 111 views

回答

0
StringBuffer postData = new StringBuffer(); 

      httpConn = (HttpConnection) Connector.open(URL); 
      httpConn.setRequestMethod(HttpConnection.POST); 

      postData.append("?username="+username); 
      postData.append("&password="+pass); 
      postData.append("&projectcode="+projectid); 
      String encodedData = postData.toString(); 

      httpConn.setRequestProperty("Content-Language", "en-US"); 
      httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
      httpConn.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString()); 
      byte[] postDataByte = postData.toString().getBytes("UTF-8"); 

      OutputStream out = httpConn.openOutputStream(); 
      out.write(postDataByte); 
      out.close(); 

      httpConn.getResponseCode(); 

      is = httpConn.openInputStream(); 

      StringBuffer buffer = new StringBuffer(); 
      int ch = 0; 

      while (ch != -1) { 
       ch = is.read(); 
       buffer.append((char) ch); 
      } 

      String json = buffer.toString(); 

      Dialog.alert("Received Json: "+json); 
+0

有幾件事情,我不喜歡,但最糟糕的是,爲了使這種代碼發出Dialog.alert(..),它並末,它必須是運行在事件線程上,當然它不應該這樣做,因爲它阻塞了網絡活動。這不是生產代碼。 –

+0

@PeterStrange這只是爲了調試目的,在屏幕上顯示輸出。是的,這不是我同意的產品代碼 –