如何在java中调用同步web服务..?

时间:2014-11-04 05:11:16

标签: java android blackberry

我是javascript guy.I知道有关闭保存参考。实际上我会解释我需要发送同步webservice请求的问题。实际上我正在从数据库中读取数据并在服务器上发布数据。我需要这个过程应该逐行。但由于线程我的所有请求同时进行。我可以发送i变量的引用(for循环)。换句话说,我的值增加当我从服务器得到响应时。 //向服务器发送请求

for (int i = fsRecord; i > 0; i--) {
    JSONObject jsonObj = dataAccess.getRecordFromTable(TABLE_FS_REPORTS, i);
    synchronized (UiApplication.getEventLock()) {
        loaderScreen=null;
        loaderScreen = new WaitScreen(STANDARD_F_AND_S_REPORT_KEY,i);
        loaderScreen.setObserver(this);
        logWarn("-----------------f and s"+jsonObj);
        Helper.HttpPOSTStream(Helper.fsReportUrl,loaderScreen,jsonObj);
    }
}

//从数据库中获取数据

public JSONObject getRecordFromTable(String tableName, int recId){
    logWarn("getRecordFromTable function call ===============");
    JSONObject obj = new JSONObject();

    try {
        logWarn("before query ===============");
        String query = "SELECT * FROM " + tableName + " WHERE id=" + recId;
        logWarn("after  query ==============="+query);
        logWarn("before   statement ==============="+query);
        Statement statement = conn.createStatement(query);
        logWarn("after   statement ==============="+query);
        statement.prepare();
        Cursor cursor = statement.getCursor();
        Row row;
    } catch (Exception e) {
        logWarn(" Exception in  getRecordFromTable ========="+e.getMessage());
    }

//发布请求

static public void HttpPOSTStream(final String fileToGet, final ResponseCallback msgs,JSONObject json) {
    final JSONObject jsonObj=json;
    Thread t = new Thread(new Runnable() {
        public void run() {

            HttpConnection hc = null;
            DataInputStream din = null;
            try {

                hc = (HttpConnection) Connector.open(fileToGet+NetworkAvailabilityCheck.getUrl());

                hc.setRequestMethod(HttpsConnection.POST);
                hc.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                hc.setRequestProperty("Accept", "application/json");
                hc.setRequestProperty("Content-Length", Integer.toString(jsonObj.toString().length()));
                DataOutputStream openDataOutputStream = hc.openDataOutputStream();
                byte[] outputBytes = jsonObj.toString().getBytes("UTF-8");
                openDataOutputStream.write(outputBytes);
                din = hc.openDataInputStream();
                ByteVector bv = new ByteVector();
                int i = din.read();
                while (-1 != i) {
                    bv.addElement((byte) i);
                    i = din.read();
                }
                final String response = new String(bv.toArray(), "UTF-8");
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        msgs.callback(response);
                    }
                });
            } catch (final Exception e) {
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Dialog.alert("Some error occured.please try again");
                        msgs.callback("Exception (" + e.getClass() + "): " + e.getMessage());
                    }
                });
            } finally {
                try {
                    din.close();
                    din = null;
                    hc.close();
                    hc = null;
                } catch (Exception e) {
                    Dialog.alert("Some error occured.please try again");
                }
            }
        }
    });
    t.start();
}

//在服务器上获得回复

public void onResponseReceived(String response, int key,int valueOfI) {
// response 
}

我需要同步请求,当我的值为0时,它调用webservice并获得响应,而不是增加i的值。

0 个答案:

没有答案
相关问题