简体   繁体   English

使用POST方法REST API进行android登录

[英]android login using POST method REST API

I am a beginner in android development and I am working on a android application with REST api for login. 我是android开发的初学者,正在使用REST api进行android应用程序登录。 I have to use POST method for login. 我必须使用POST方法登录。 After going through the docs and sites, i tried to implement the below code, but It's giving "invalid post request" every time. 在浏览了文档和网站之后,我尝试实现以下代码,但是每次都给出“无效的发布请求”。 I tried to debugge but wasn't able to find the reason. 我尝试调试,但找不到原因。 Can someone please help me with link to understand how can i implement this. 有人可以通过链接帮助我,以了解如何实现此目标。 We have to pass the JSON paramter {"username": "abc@test.com","password": "abctest"} (i guess that is general) 我们必须传递JSON参数{“ username”:“ abc@test.com”,“ password”:“ abctest”} (我想这很一般)

  HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://beta.m-adaptive.com/login"); BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("username", paramUsername); BasicNameValuePair passwordBasicNameValuePair = new BasicNameValuePair("password", paramPassword); List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); nameValuePairList.add(usernameBasicNameValuePair); nameValuePairList.add(passwordBasicNameValuePair); try { UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList); httpPost.setEntity(urlEncodedFormEntity); try { HttpResponse httpResponse = httpClient.execute(httpPost); InputStream inputStream = httpResponse.getEntity().getContent(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String bufferedStrChunk = null; while((bufferedStrChunk = bufferedReader.readLine()) != null){ stringBuilder.append(bufferedStrChunk); } return stringBuilder.toString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); if(result.equals("working")){ Toast.makeText(getApplicationContext(), "working...", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(getApplicationContext(), "Invalid POST request...", Toast.LENGTH_LONG).show(); } 

If you want to pass JSON as your only parameter, you have to use StringEntity instead of UrlEncodedFormEntity and send the whole string, like this: 如果要传递JSON作为唯一参数,则必须使用StringEntity而不是UrlEncodedFormEntity并发送整个字符串,如下所示:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://beta.m-adaptive.com/login");
httpPost.setEntity(new StringEntity(YOUR JSON));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
try {
    HttpResponse httpResponse = httpClient.execute(httpPost);
    ...

May be this helps : 也许这会有所帮助:

class PlaceOrder extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            // TODO Auto-generated method stub

            try {

                HttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPst = new HttpPost(

                "yout_url");

                ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>(

                2);

                parameters.add(new BasicNameValuePair("username", "apple"));

                parameters.add(new BasicNameValuePair("pw", "apple"));

                parameters.add(new BasicNameValuePair("email",
                        "apple@gmail.com"));

                parameters.add(new BasicNameValuePair("name", "apple"));

                httpPst.setEntity(new UrlEncodedFormEntity(parameters));

                HttpResponse httpRes = httpClient.execute(httpPst);

                String str = convertStreamToString(
                        httpRes.getEntity().getContent()).toString();

                Log.i("mlog", "outfromurl" + str);

            } catch (UnsupportedEncodingException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            } catch (ClientProtocolException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            return null;

        }

    }

    public static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line = null;

        try {

            while ((line = reader.readLine()) != null) {

                sb.append(line + "\n");

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                is.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return sb.toString();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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