2014-05-02 63 views
1
  1. 我創建了一個WCF,它帶有2個參數用戶名和密碼。
  2. 我已經創建了一個android用戶界面,其中輸入用戶名和密碼,並在按鈕單擊事件它的用戶名和密碼存在我已經分配了一個靜態值「登錄成功」標籤。

我需要指導如何將結果集與標籤綁定,而不是將靜態值傳遞給標籤。WCF與Android-綁定結果與labe或列表視圖

public class JSONSampleAppActivity extends Activity實現OnClickListener { /**第一次創建活動時調用。 */

//Property declaration 
Button btnLogin; 
TextView lblStatus; 
EditText txtUserName,txtPassword; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnLogin=(Button)findViewById(R.id.btnLogin); 
    btnLogin.setOnClickListener(this); 

    lblStatus=(TextView)findViewById(R.id.lblStatus); 

    txtUserName=(EditText)findViewById(R.id.txtUserName); 
    txtPassword=(EditText)findViewById(R.id.txtPassword); 
} 

@Override 
public void onClick(View v) { 

    switch(v.getId()) 
    { 
     case R.id.btnLogin: 
      String userName=txtUserName.getText().toString(); 
      String password=txtPassword.getText().toString(); 
      if(verifyLogin(userName,password)) 
      { 

       lblStatus.setText("Login Successfully"); 
      } 
      else 
      { 
       lblStatus.setText("Login Failed"); 
      } 
      break; 
    } 
} 

public static String convertStreamToString(InputStream is) 
{ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

public static boolean verifyLogin(String UserName,String Password) 
{ 
    try 
    { 
     System.out.println("guru"); 
     DefaultHttpClient httpClient=new DefaultHttpClient(); 

     //Connect to the server 
     HttpGet httpGet=new HttpGet("http://xxxx/Service1.svc/checkLogin?name="+UserName+"&pass="+Password); 
     //Get the response 
     HttpResponse httpResponse = httpClient.execute(httpGet); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream stream=httpEntity.getContent(); 

     //Convert the stream to readable format 
     String result= convertStreamToString(stream); 

     if(result.charAt(1)=='1') 
     { 

      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    catch(Exception e) 
    { 
     return false; 
    } 

} 

回答

2
To get the result set in label your code should look like this : 

public static String verifyLogin(String UserName,String Password) 
{ 
    try 
    { 
     System.out.println("guru"); 
     DefaultHttpClient httpClient=new DefaultHttpClient(); 

     //Connect to the server 
     HttpGet httpGet=new HttpGet("http://xxxx/Service1.svc/checkLogin?name="+UserName+"&pass="+Password); 
     //Get the response 
     HttpResponse httpResponse = httpClient.execute(httpGet); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream stream=httpEntity.getContent(); 

     //Convert the stream to readable format 
     String result= convertStreamToString(stream); 



    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return result; 
} 

call above method like this in onclick: 

      String userName=txtUserName.getText().toString(); 
      String password=txtPassword.getText().toString(); 
      String res=verifyLogin(userName,password) 
      lblStatus.setText(res); 

whatever result is will be displayed in textview. 
+0

我有我的編輯代碼,如你所說,但它給錯誤 – user3597236

+0

OK,你必須把返回結果; catch block後 –

+0

@ imran betara這個wcf返回多於一行,每行有2列。通過上面的代碼,它可以連接所有列的值並將其與一個標籤綁定。我想用label2將第一列值與label1和第二列值綁定,而不是連接。我怎樣才能完成這項任務。 – user3597236