2012-07-07 3 views
0

모바일 웹 사이트 로그인 실패 또는 성공에 대한 응답을 받으려고합니다. 여기 내 코드의 일부입니다.Android Jsoup 로그인 응답 받기

편집


public String login(String User, String Pass) throws Exception{ 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 


    HttpPost httppost = new HttpPost(AmwayURL); 
    String result = "nothing"; 
    try { 
     // Add user name and password 
     String username = User; 
     String password = Pass; 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("userid", username)); 
     nameValuePairs.add(new BasicNameValuePair("userpswd", password)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     CookieStore cookieStore = new BasicCookieStore(); 
     HttpContext localContext = new BasicHttpContext(); 
     localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

     HttpResponse response = httpclient.execute(httppost, localContext); 

     result = inputStreamToString(response.getEntity().getContent()).toString(); 

     HttpEntity entity = response.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 
     if (entity != null) { 
      System.out.println("Response content length: " + entity.getContentLength()); 
     } 
     List<Cookie> cookies = cookieStore.getCookies(); 
     for (int i = 0; i < cookies.size(); i++) { 
      System.out.println("Local cookie: " + cookies.get(i)); 
     } 

     // Consume response content 
     //EntityUtils.consume(entity); 

     System.out.println("----------------------------------------"); 



    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    } 
    return result; 
} 

내가 응답을받을 수 있나요 그리고 내가 그것을 조사하면 내가 로그인 perfomed 않았 음을 볼 수 있지만 내 질문이 있는지 확인하기 위해 프로그래밍 방법을 사용할 수있는 방법입니다 로그인은 성공적 이었습니까?

해당 사이트는 동일한 페이지에 있으므로 리디렉션 코드를 사용할 수 없습니다.

+0

당신이 어떤 문제가 발생했는지 설명 할 수 :

http://jsoup.org/cookbook/input/parse-document-from-string

당신은 항상 할 수있는 특정 요소를 분석해야하는 경우

? 로그인 할 수 없거나 로그인 할 수는 있지만 응답을받을 수 없습니까? –

+0

내 코드를 수정하고 응답을 얻을 수 있지만 작업이 성공했는지 확인하는 프로그래밍 방식을 알고 싶습니다. 예를 들어 특정 요소가 더 이상 존재하지 않는지 확인하는 방법입니다. –

+0

Jsoup를 사용하여 응답을 구문 분석해야합니다. 요소를 선택하면 null이 반환되고 요소는 없습니다. –

답변

0

문자열에 대한 응답을 전달한 다음 구문 분석을 위해 공식 문서를 따르십시오!

doc.select(your CSS query here searching a tag, element, class, id or even some atribute value)