在Android中向服务器发送用户名和密码并获得响应

在Android中向服务器发送用户名和密码并获得响应,android,Android,如何在Android中使用httpclient向服务器发送用户名和密码并从服务器获得响应?我研究了很多网站,但我找不到我想要的任何东西。制作一个类适配器并将此代码放在这里……我假设您使用的是json webService public String login(String uname, String password) { InputStream is = null; String result = ""; ArrayList<NameValuePair>

如何在Android中使用httpclient向服务器发送用户名和密码并从服务器获得响应?我研究了很多网站,但我找不到我想要的任何东西。

制作一个类适配器并将此代码放在这里……我假设您使用的是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;
}

试试这个,它可能对你有帮助

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

您必须在服务器端创建一个web服务


然后,至少有两种方法可以将数据发送到服务器并从代码中获得响应。搜索
HttpPost
HttpUrlConnection
。第一种方法更易于使用POST方法发送数据,但速度也慢得多。

您的问题并不清楚。什么用户名和密码?什么服务器?您希望得到什么样的响应?您必须在服务器端创建web服务。然后,至少有两种方法可以将数据发送到服务器并从代码中获得响应。搜索
HttpPost
HttpUrlConnection
。第一种更容易使用,效果非常好。谢谢..@mehul ranpara如何使用数组发送
emailid
password
。例如
user\u auth
@Sameera您可以将数组传递给这个类,并从中获取值,例如:L arr.get(0)--user\u auth或arr.get(1)--user\u password等@mehul ranpara您能在这里帮助我吗
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
     }
   }
}
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 "";
}