2

Androidでhttpclientを使用して、ユーザー名とパスワードをサーバーに送信し、サーバーから応答を取得するにはどうすればよいですか? たくさんのウェブサイトを調べましたが、欲しいものが見つかりませんでした。

4

3 に答える 3

9

クラスAdapterを作成し、このコードを配置します...ここでは、jsonwebServiceを使用していると想定しています。

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());
于 2013-01-19T11:26:11.243 に答える
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 "";
}
于 2013-01-19T11:26:14.833 に答える
0

サーバー側で Web サービスを作成する必要があります。

次に、データをサーバーに送信し、コードから応答を取得する方法が少なくとも 2 つあります。HttpPost&を検索しHttpUrlConnectionます。最初の方法はPOSTメソッドでデータを送信するのに使いやすいですが、非常に遅くなります。

于 2013-01-19T11:35:02.513 に答える