簡體   English   中英

HTTP Put請求Android REST API

[英]HTTP Put request Android REST API

我是Android開發的新手,目前的任務是實現一個簡單的登錄活動,該活動將驗證REST后端服務的電子郵件/密碼組合。 我配置了布局,並在活動類中配置了EditText電子郵件和密碼的值,但是我非常想嘗試如何提出HTTP Put請求...非常感謝您的幫助。 我的課和verifyLogin在下面。

public class LoginActivity extends ActionBarActivity {

public void verifyLogin(View view) {

    EditText emailEditText = (EditText)         findViewById(R.id.editText_email_address);
    EditText passwordEditText = (EditText) findViewById(R.id.editText_password);
    String email = emailEditText.getText().toString();
    String password = passwordEditText.getText().toString();

    boolean loginSuccess = false;
    //URL for backend login service
    URL url = new URL("http://ENDPOINT_LOGIN_URL_HERE");
    //put request for this URL
    HttpPut httpPut = new HttpPut(url);
    httpPut.addHeader("application/xml", "Content_type");
    DefaultHttpClient httpClient = new DefaultHttpClient();
    //response from backend
    HttpResponse response = httpClient.execute(httpPut);

}

PS:我已經做了大量的Google搜索,並且觀看了視頻並瀏覽了示例代碼。 如果沒有別的,一個好的起點將是一個巨大的幫助,甚至是一個尋求幫助的好地方。

我正在使用Apache HttpClient for Android。 Android在SDK中具有此庫。 有很多關於此的教程。 檢查: http : //www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

基本代碼是:

  HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("your url"); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("postFiledName1", "value1")); pairs.add(new BasicNameValuePair("postFiledName2", "value2")); post.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(post); // then do something about the response 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM