2013-01-19 103 views
2

如何將我的用戶名和密碼發送到服務器,並在Android中使用httpclient從服務器獲得響應?我研究了很多網站,但我找不到任何我想要的東西。發送用戶名和密碼到服務器並在Android中獲得響應

+2

你的問題並不清楚。 什麼用戶名和密碼?什麼服務器?你期待什麼反應? –

+0

您必須在服務器端進行** web服務**。然後至少有兩種方法將數據發送到服務器並從代碼中獲得響應。搜索「HttpPost」和「HttpUrlConnection」。第一個更容易使用。 – Flawyte

回答

9

製作一個類適配器,並把這個代碼...在這裏,我假設你正在使用json webService。

public String login(String uname, String password) 
{ 
    InputStream is = null; 
    String result = ""; 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("Your Url"); 

     nameValuePairs.add(new BasicNameValuePair("emailid", uname)); 
     nameValuePairs.add(new BasicNameValuePair("password", password)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 

     is = entity.getContent(); 
    } 
    catch (Exception e) 
    { 
     Log.e("log_tag", "Error in http connection " + e.toString()); 
    } 

    // convert response to string 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line).append("\n"); 
     } 

     is.close(); 
     result = sb.toString(); 

     Log.v("log","Result: " + result); 
    } 
    catch (Exception e) 
    { 
     Log.v("log", "Error converting result " + e.toString()); 
    } 

    return result; 
} 

做出這樣的電話...

Adapter adb = new Adapter(); 
response = adb.login(edtLoginemail.getText().toString(), edtLoginPassword.getText().toString()); 
+0

工作得很好。謝謝.. – bynu022

+0

@ mehul-ranpara我怎樣才能發送'emailid'和'密碼'使用array.eg ..'user_auth' – kosala

+1

@Sameera你可以傳遞數組到這個類,並從那裏你可以採取價值爲前:L arr .get(0) - user_auth或arr.get(1) - user_password等 –

0

試試這個它可以幫助你。

userId = editTextUserName.getText().toString(); 
password = editTextPassword.getText().toString(); 

String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
      + "<login><User>" + userId + "</User>" 
      + "<Password>" + password + "</Password></login>"; 
      serverCall(xml); 


serverCall(xml); 


private void serverCall(final String xml) 
{ 

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
if(conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected()) 
    { 

    result = new Connection().getResponse("http://localhost/login.php" , xml); 
    if(result.equals("yes") { 
     //registration done 
    } else { 
     //failed 
    } 
    } 
} 

Connection.java

public String getResponse(String connUrl, String xml) { 
    DefaultHttpClient httpClient = null; 
    try { 
     MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     HttpPost method = new HttpPost(connUrl); 
     method.setEntity(mp); 
     mp.addPart("xml_request", new StringBody(xml)); 

     httpClient = new DefaultHttpClient(); 
     HttpResponse response = httpClient.execute(method); 

     if(response.getStatusLine().getStatusCode() == 200){ 
      ByteArrayOutputStream outstream = new ByteArrayOutputStream(); 
      response.getEntity().writeTo(outstream); 
      return outstream.toString(); 
     } 
    } 
    catch(Exception e) { 
     Log.v("APP", "Exception while create connection for getResponse from server with: " + e.getMessage()); 
    } 
    finally { 
     httpClient.getConnectionManager().shutdown(); 
    } 
    return ""; 
} 
0

你必須要對服務器端的Web服務。

然後至少有兩種方法將數據發送到服務器並從您的代碼獲得響應。搜索HttpPost & HttpUrlConnection。第一種方法更容易使用POST方法發送數據,但也很慢。

相關問題