简体   繁体   中英

HTTP Put request Android REST API

I am fairly new to Android development, and my current task is to implement a simple login activity that verifies an email/password combination with a REST backend service. I have the layout configured, as well as the values for the EditText email and password in my activity class, but I am very stuck trying to figure out how to make the HTTP Put requests... Any help would be greatly appreciated. My class and verifyLogin are below.

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: I have done a great, great deal of Google searches and have watched videos and looked through sample code. If nothing else, a good starting point would be a great help or even a good place to look for help.

I am using Apache HttpClient for Android. Android has this library inside the SDK. There are many tutorials talking about this. Check: http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

The basic codes are:

  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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM