Android: IllegalStateException in HttpGet

前端 未结 4 1623
甜味超标
甜味超标 2020-12-02 02:58

I\'m trying to send a GET request using HttpClient, but I keep getting an IllegalStateException. Any idea what\'s causing this? I\'ve

相关标签:
4条回答
  • 2020-12-02 03:24

    This is your Host error You are passing "www.host.com" instead you have to pass "http://www.w3schools.com"

    reference post is here

    0 讨论(0)
  • 2020-12-02 03:32

    When you do this:

    HttpGet getData = new HttpGet(this.address);
    

    what exactly is this.address? I'm assuming it is a String, if so it needs to look like this:

    String address = "http://www.google.com/path/to/document";
    

    You've probably done this:

    String address = "google.com";
    
    0 讨论(0)
  • 2020-12-02 03:41

    I think you need to use .addHeader for HttpGet, for example to transfer data in Json format:

    public class Client {
    
        private String server;
    
        public Client(String server) {
            this.server = server;
        }
    
        private String getBase() {
            return server;
        }
    
        public String getBaseURI(String str) {
            String result = "";
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet getRequest = new HttpGet(getBase() + str);
                getRequest.addHeader("accept", "application/json");
                HttpResponse response = httpClient.execute(getRequest);
                result = getResult(response).toString();
                httpClient.getConnectionManager().shutdown();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        private StringBuilder getResult(HttpResponse response) throws IllegalStateException, IOException {
    
            StringBuilder result = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())), 1024);
            String output;
            while ((output = br.readLine()) != null) 
                result.append(output);
            return result;
        }
    }
    

    I also suggest you to use timeoutConnection (until a connection is established) and timeoutSocket (timeout for waiting for data.) for more information look at: Java jersey RESTful webservice requests

    Also note that You need to implement your network connection on a separate thread for API level 11 or greater.

    0 讨论(0)
  • 2020-12-02 03:41

    you have to add the http:// to address, in order to get it work. Avoid the use of URLEncoder, if you do.

    0 讨论(0)
提交回复
热议问题