android登录使用POST方法REST API

时间:2014-08-26 12:53:30

标签: android api rest post

我是android开发的初学者,我正在使用REST api进行登录的Android应用程序。我必须使用POST方法登录。 在浏览了文档和网站后,我尝试实施以下代码,但它提供了"无效的帖子请求"每次。我试图调试,但无法找到原因。有人可以帮我链接,了解我如何实现这一点。 我们必须传递JSON参数 {"用户名":" abc@test.com","密码":" abctest& #34;} (我猜这是一般的)

          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();
            }

2 个答案:

答案 0 :(得分:0)

如果要将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);
    ...

答案 1 :(得分:0)

可能有帮助:

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();

    }