2

JSONを使用してWebサービスアプリケーションに取り組んでいます。タスクを実行する際に、URLを押すことでJSOn応答を直接フェッチすることに成功しました。

これで、リクエストパラメータを使用してリクエストするタスクがあります。

enter code here
private void callJSON_Webservice(String method,String paraLastModifiedDate) {
        HttpConnection c=null;
        InputStream is = null;
        String feedURL = Constants.feedURL;
        int rc;

        try{
            JSONObject postObject = new JSONObject();
            postObject.put("CheckLatestDataDate",method);
            postObject.put("LastModifiedDate", paraLastModifiedDate);
            //c = new HttpConnectionFactory().getHttpConnection(feedURL);
            c = (HttpConnection)Connector.open(feedURL + ConnectionManager.getConnectionString());

            // Set the request method and headers
            c.setRequestMethod(HttpConnection.GET);
            c.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            c.setRequestProperty("Content-Length", "" + (postObject.toString().length() - 2));
            //c.setRequestProperty("method", HttpConnection.GET);

            // 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();

            String json = StringUtils.convertStreamToString(is);
            object = new JSONObject(json);


        }catch (Exception e) {
            System.out.println(e+"call webservice exception");
        }

    }

このコードでは、EOF例外が発生しています。この小さなタスクをできるだけ早く完了する必要があります。私を助けてください...!よろしくお願いします

4

1 に答える 1

2

交換してみてください

is = c.openInputStream();

String json = StringUtils.convertStreamToString(is);

次のように:

is = c.openInputStream();

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

String json = buffer.toString();


参照:StreamConnectionを文字列に変換

于 2012-05-20T18:09:31.180 に答える