-3

Java code:

try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
             HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse =httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

where url is like:

http://122.180.133.121:84/diogo/api/api.php?class=authenticate&method=login_check&param={'userpassword':'777','username':'www'}

and logcat error is:

java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://122.170.103.168:86/diogo/api/api.php?class=authenticate&method=login_check&param={'userpassword':'123','username':'krunal'}

I already searched many solutions but all are saying about http or www.. but i dont know what is the actual problem. is just because of my server ip address or what.?

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99

2 Answers2

1

Change url encoding method. try this method. it must be url encoding problem-

stringByAddingPercentEscapesUsingEncoding(urlString);

public static String stringByAddingPercentEscapesUsingEncoding(String input) {
    try {
        return stringByAddingPercentEscapesUsingEncoding(input, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(
                "Java platforms are required to support UTF-8");
        // will never happen
    }
}

public static String stringByAddingPercentEscapesUsingEncoding(
        String input, String charset) throws UnsupportedEncodingException {
    byte[] bytes = input.getBytes(charset);
    StringBuilder sb = new StringBuilder(bytes.length);
    for (int i = 0; i < bytes.length; ++i) {
        int cp = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
        if (cp <= 0x20
                || cp >= 0x7F
                || (cp == 0x22 || cp == 0x25 || cp == 0x3C || cp == 0x3E
                        || cp == 0x20 || cp == 0x5B || cp == 0x5C
                        || cp == 0x5D || cp == 0x5E || cp == 0x60
                        || cp == 0x7b || cp == 0x7c || cp == 0x7d)) {
            sb.append(String.format("%%%02X", cp));
        } else {
            sb.append((char) cp);
        }
    }
    return sb.toString();
}
Priyanka
  • 677
  • 5
  • 20
0

URIUtils.extractHost(final URI uri) can not get the target host because param contains illegal characters first located at index 88, {.

If you encode your complete URL with the default Java URLEncoder the resulting URL is

http%3A%2F%2F122.180.133.121%3A84%2Fdiogo%2Fapi%2Fapi.php%3Fclass%3Dauthenticate%26method%3Dlogin_check%26param%3D%7B%27userpassword%27%3A%27777%27%2C%27username%27%3A%27www%27%7D

but you can not use that URL because de default URI Java class can not parse it and obtain the scheme, host... etc

The solution, parse the illegal content only and pass to your URL parameter

String param = URLEncoder.encode("{'userpassword':'777','username':'www'}",
"UTF-8");

HttpGet request = new HttpGet("http://122.180.133.121:84/diogo/api/api.php?
class=authenticate&method=login_check&param=" + param);
vzamanillo
  • 9,905
  • 1
  • 36
  • 56