Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Falcon RESTful API获取URI_Java_Httpwebrequest_Asp.net Web Api - Fatal编程技术网

Java 如何使用Falcon RESTful API获取URI

Java 如何使用Falcon RESTful API获取URI,java,httpwebrequest,asp.net-web-api,Java,Httpwebrequest,Asp.net Web Api,我正在使用FALCON语义搜索引擎RESTful API&编写了这个程序 但是没有得到搜索引擎应该响应的结果。请查看代码并帮助我 package httpProject; import java.io.*; import java.net.*; import java.lang.*; public class HTTPRequestPoster { public String sendGetRequest(String endpoint, String requestParamete

我正在使用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();
    }
}

除了两个小问题外,你已经完成了所有的繁重工作:

  • 此处不应使用urlcoder.encode(…)。表示它将字符串转换为
    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);
}

除了两个小问题外,你已经完成了所有的繁重工作:

  • 此处不应使用urlcoder.encode(…)。表示它将字符串转换为
    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);
}

这不是您的问题,但您永远不需要执行
importjava.lang.*正如Java为您自动完成的那样。始终具有.ya这是正确的,但在删除import java.lang.*后它将不起作用;把它拿走后就不行了?!要么你的类和
HTTPRequestPoster
在同一个包中,名称选择得很糟糕,要么这个世界真的很奇怪。这不是你的问题,但你永远不需要做
import java.lang.*正如Java为您自动完成的那样。始终具有.ya这是正确的,但在删除import java.lang.*后它将不起作用;把它拿走后就不行了?!要么你的类与
HTTPRequestPoster
在同一个包中,名称选择得非常糟糕,或者这个世界真的很奇怪。谢谢你的建议,但我需要用UTF-8格式编码字符串。这是Java,所以你不需要显式地这么做。谢谢你的建议,但我需要用UTF-8格式编码字符串。这是Java,所以你不需要显式地这么做。