User3
User3

Reputation: 2535

Http Url Connection error

I am getting the following error:

java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=CONNECT_URL

Following are my Global variables:

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; 

following is my parse function:

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); 
            }
        }



    }

I tried to refator it with other similar questions on SO, but my URL seems to be okay and it returns the JSON once I check the same URL from a browser, any hints?

Upvotes: 0

Views: 270

Answers (4)

Shlublu
Shlublu

Reputation: 11027

I think the problem comes from this line:

HttpPost httppost = new HttpPost("CONNECT_URL");

You are passing the string "CONNECT_URL" instead of passing the variable CONNECT_URL :)

Upvotes: 1

Gabriel Negut
Gabriel Negut

Reputation: 13960

HttpPost httppost = new HttpPost("CONNECT_URL");

should be

HttpPost httppost = new HttpPost(CONNECT_URL);

As a side note, Java convention dictates that variables are camel case (connectUrl) and constants are uppercase (CONNECT_URL).

Upvotes: 1

Vimal Bera
Vimal Bera

Reputation: 10497

You are passing "CONNECT_URL" in HttpPost object which is wrong. Use

HttpPost httppost = new HttpPost(CONNECT_URL) //instead of HttpPost("CONNECT_URL")

Upvotes: 1

reixa
reixa

Reputation: 7011

You´ve got

HttpPost httppost = new HttpPost("CONNECT_URL");

and looking to your code should be like

HttpPost httppost = new HttpPost(CONNECT_URL);

Upvotes: 1

Related Questions