Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConnectionThread extends Thread
- {
- LoadingScreen loadingScreen;
- String url;
- HttpConnection httpConnection;
- InputStream inputStream;
- String id="0";
- StringBuffer stringBuffer=new StringBuffer();
- public ConnectionThread(LoadingScreen loadingScreen, String url)
- {
- this.loadingScreen=loadingScreen;
- this.url=url;
- }
- public void run()
- {
- try
- {
- httpConnection=(HttpConnection)Connector.open("http://startlogic.totemcat.net/sika/WebServices/sikainfo.php"+";interface=wifi");
- URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
- oPostData.append("category",id);
- System.out.println("================"+oPostData.toString());
- httpConnection.setRequestMethod(HttpConnection.POST);
- httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType());
- byte [] postBytes = oPostData.getBytes();
- httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
- OutputStream strmOut = httpConnection.openOutputStream();
- strmOut.write(postBytes);
- strmOut.flush();
- strmOut.close();
- int response=httpConnection.getResponseCode();
- if(response==HttpConnection.HTTP_OK)
- {
- inputStream = httpConnection.openInputStream();
- int c;
- while((c=inputStream.read())!=-1)
- {
- stringBuffer.append((char)c);
- }
- // System.out.println(">>>>>>>>>>>>>>>>>"+stringBuffer.toString());
- callBack(stringBuffer.toString());
- }
- else
- {
- callBack("ERROR");
- }
- }
- catch (Exception e)
- {
- synchronized (UiApplication.getEventLock())
- {
- UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
- StartUp.exceptionHandling(e.getMessage());
- }
- }
- finally
- {
- try
- {
- if(httpConnection!=null)
- {
- httpConnection.close();
- }
- if(inputStream!=null)
- {
- inputStream.close();
- }
- }
- catch (Exception e2)
- {}
- }
- }
- private void callBack(String response)
- {
- if(response.equals("ERROR"))
- {
- UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
- StartUp.exceptionHandling("URL Not found");
- }
- else
- {
- try
- {
- System.out.println(response);
- //do what you want;
- }
- catch (Exception e)
- {
- StartUp.exceptionHandling("Data Not found");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement