2014-04-02 4 views
-3

자바 코드가 아니어야합니다 :대상은 널 (null)

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(); 
     } 
URL이 같은입니다

:

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

및 로그 캣 오류는 다음과 같습니다

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'} 

이미 많은 솔루션을 검색하지만, 모두가 http 또는 www ..에 대해 말하는하지만 실제 문제는 무엇인지 모르겠다. 그냥 내 서버의 IP 주소 또는 무엇 때문입니다.?

+0

이 있는지 확인 불법적 인 내용 만 분석하고 URL 매개 변수로 전달의 유효한 URL – Raghunandan

+0

나는 이미 그것을 확인했다. – Riser

+0

URL 인코딩을 시도하십시오. wild though guys. – Raghunandan

답변

1

URL 인코딩 방법을 변경하십시오. 이 방법을 시도하십시오. 그것은 URL 인코딩 problem-

stringByAddingPercentEscapesUsingEncoding (URLString는)이어야; param는 첫 번째 인덱스 88, {에있는 잘못된 문자가 포함되어 있기 때문에

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(); 
} 
+0

그게 작동하지 않습니다 .. : P – Riser

+0

같은 문제가 여전히오고 ??? – Priyanka

+0

동일한 문제가 아니지만 제대로 인코딩되지 않습니다 .. – Riser

0

URIUtils.extractHost(final URI uri)은 대상 호스트를 얻을 수 없습니다. 기본 자바 URLEncoder으로 전체 URL을 인코딩하는 경우

는 결과 URL은

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 

하지만 드 기본 URI 자바 클래스는, 호스트를 분석하고 계획을 얻을 수 없기 때문에 해당 URL을 사용할 수 없습니다. 등등

솔루션,

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