1

私は FALCON セマンティック検索エンジン RESTful API を使用しており、このプログラムを作成しましたが、検索エンジンから応答するはずの結果が得られません。コードを見て助けてください。

package httpProject;

import java.io.*;
import java.net.*;
import java.lang.*;

public class HTTPRequestPoster {
    public String sendGetRequest(String endpoint, String requestParameters) {
        String result = null;
        if (endpoint.startsWith("http://")) {
            try {
                String urlStr = endpoint;
                if (requestParameters != null && requestParameters.length () > 0) {
                    urlStr += "?" + requestParameters;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection ();

                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
                rd.close();
                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * @param args
     * @throws UnsupportedEncodingException 
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        //HTTPRequestPoster a = new HTTPRequestPoster();//
        HTTPRequestPoster astring = new HTTPRequestPoster ();
        String param = "query=Person";
        String stringtoreverse = URLEncoder.encode(param, "UTF-8");
        astring.sendGetRequest("http://ws.nju.edu.cn/falcons/api/classsearch.jsp", stringtoreverse);
        astring.toString();

        System.out.println(astring);
        //PrintStream.class.toString();
    }
}
4

1 に答える 1

1

2 つの小さな問題を除いて、すべての面倒な作業を完了しました。

  • URLEncoder.encode(...)ここでは使用しないでください。Javadocは、文字列をapplication/x-www-form-urlencodedフォーマットに変換すると言いPOSTます。

  • astring.sendGetRequest(...)astringそれ自体の代わりに結果として使用する必要があります。

次の作品:

public static void main(String[] args) throws UnsupportedEncodingException {
    // TODO Auto-generated method stub
    //HTTPRequestPoster a = new HTTPRequestPoster();//
    HTTPRequestPoster astring = new HTTPRequestPoster ();
    String param = "query=Person";
    String result = astring.sendGetRequest("http://ws.nju.edu.cn/falcons/api/classsearch.jsp", param);

    System.out.println(result);
}
于 2011-12-24T13:59:38.820 に答える