Java Http Url连接错误
我得到以下错误:Java Http Url连接错误,java,android,http,Java,Android,Http,我得到以下错误: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=CONNECT_URL 以下是我的全局变量: String CONNECT_URL = "http://api.openweathermap.org/data/2.5/weather?q=Mumbai"; int LAST_INDEX;
java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=CONNECT_URL
以下是我的全局变量:
String CONNECT_URL = "http://api.openweathermap.org/data/2.5/weather?q=Mumbai";
int LAST_INDEX;
String NAME;
String TYPE;
String GREETING_YEAR;
String GREETING_GENERAL;
String RADIO_TYPE;
InputStream ins = null;
String result = null;
以下是我的解析函数:
public void parse(){
DefaultHttpClient http = new DefaultHttpClient(new BasicHttpParams());
System.out.println("URL is: "+CONNECT_URL);
HttpPost httppost = new HttpPost("CONNECT_URL");
httppost.setHeader("Content-type", "application/json");
try{
HttpResponse resp = http.execute(httppost);
HttpEntity entity = resp.getEntity();
ins = entity.getContent();
BufferedReader bufread = new BufferedReader(new InputStreamReader(ins, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = bufread.readLine()) != null){
sb.append(line +"\n");
}
result = sb.toString();
System.out.println("Result: "+result);
}catch (Exception e){
System.out.println("Error: "+e);
}finally{
try{
if(ins != null){
ins.close();
}
}catch(Exception squish){
System.out.println("Squish: "+squish);
}
}
}
我试着用其他类似的问题来重构它,但我的URL似乎没问题,一旦我从浏览器中检查相同的URL,它就会返回JSON,有什么提示吗 你有
HttpPost httppost = new HttpPost("CONNECT_URL");
看看你的代码应该是这样的
HttpPost httppost = new HttpPost(CONNECT_URL);
你有
HttpPost httppost = new HttpPost("CONNECT_URL");
看看你的代码应该是这样的
HttpPost httppost = new HttpPost(CONNECT_URL);
您正在HttpPost对象中传递“CONNECT\uURL”,这是错误的。使用
HttpPost httppost = new HttpPost(CONNECT_URL) //instead of HttpPost("CONNECT_URL")
您正在HttpPost对象中传递“CONNECT\uURL”,这是错误的。使用
HttpPost httppost = new HttpPost(CONNECT_URL) //instead of HttpPost("CONNECT_URL")
应该是
HttpPost httppost = new HttpPost(CONNECT_URL);
作为旁注,Java约定规定变量为驼峰大小写(connectUrl
),常量为大写(CONNECT\uURL
)
应该是
HttpPost httppost = new HttpPost(CONNECT_URL);
作为旁注,Java约定规定变量为驼峰大小写(
connectUrl
),常量为大写(CONNECT\URL
)。我认为问题来自这一行:
HttpPost httppost = new HttpPost("CONNECT_URL");
您正在传递字符串
“CONNECT\u URL”
而不是传递变量CONNECT\u URL
:)我认为问题来自这一行:
HttpPost httppost = new HttpPost("CONNECT_URL");
您正在传递字符串
“CONNECT\u URL”
而不是传递变量CONNECT\u URL
:)谢谢您的友好提示,我一定会更改它:)谢谢您的友好提示,我一定会更改它:)