协议获取Java URL



我试图获得查询谷歌时发现的所有网站的JSON格式。

代码:

import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
/**
 * Created by Vlad on 19/03/14.
 */
public class Query {   
    public static void main(String[] args){
        try{
        String arg;
            arg = "random";
        URL url = new URL("GET https://www.googleapis.com/customsearch/v1?key=&cx=017576662512468239146:omuauf_lfve&q=" + arg);
        InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF-8");
    int ch;
            while((ch = reader.read()) != -1){
    System.out.print(ch);
            }
        }catch(Exception e)
        {
            System.out.println("This ain't good");
            System.out.println(e);
        }
    }    
}

例外:

java.net. malformmedurlexception: no protocol: GET https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=random

您应该删除开头的GET;)

你应该将你的代码替换为:

URL url = new URL("https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=" + arg);

Url永远不要以GETPOST或类似的东西开头;)

url应该从传输协议开始,GET https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=randomGET开始,这就是收到异常的原因。

改为https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=random

相关内容

  • 没有找到相关文章

最新更新