我收到以下错误:
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);
}
}
}
我尝试用SO上的其他类似问题来修改它,但我的URL似乎没问题,一旦我从浏览器检查相同的URL,它会返回JSON,任何提示?
答案 0 :(得分:1)
你有
HttpPost httppost = new HttpPost("CONNECT_URL");
并且查看您的代码应该像
HttpPost httppost = new HttpPost(CONNECT_URL);
答案 1 :(得分:1)
您正在HttpPost对象中传递“CONNECT_URL”,这是错误的。使用
HttpPost httppost = new HttpPost(CONNECT_URL) //instead of HttpPost("CONNECT_URL")
答案 2 :(得分:1)
HttpPost httppost = new HttpPost("CONNECT_URL");
应该是
HttpPost httppost = new HttpPost(CONNECT_URL);
作为旁注,Java约定规定变量是驼峰式的(connectUrl
),常量是大写的(CONNECT_URL
)。
答案 3 :(得分:1)
我认为问题来自这一行:
HttpPost httppost = new HttpPost("CONNECT_URL");
您传递的是字符串"CONNECT_URL"
,而不是传递变量CONNECT_URL
:)