Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用POST方法RESTAPI登录android_Android_Api_Rest_Post - Fatal编程技术网

使用POST方法RESTAPI登录android

使用POST方法RESTAPI登录android,android,api,rest,post,Android,Api,Rest,Post,我是android开发的初学者,正在开发一个android应用程序,使用RESTAPI登录。我必须使用POST方法登录。 在浏览了文档和站点之后,我尝试实现下面的代码,但每次都会发出无效的post请求。我试图调试,但找不到原因。有人可以帮助我的链接,以了解我如何才能实现这一点。 我们必须传递JSON参数{username:abc@test.com,密码:abctest}我想这是一般性的 如果要将JSON作为唯一参数传递,则必须使用StringEntity而不是UrlEncodedFormEnti

我是android开发的初学者,正在开发一个android应用程序,使用RESTAPI登录。我必须使用POST方法登录。 在浏览了文档和站点之后,我尝试实现下面的代码,但每次都会发出无效的post请求。我试图调试,但找不到原因。有人可以帮助我的链接,以了解我如何才能实现这一点。 我们必须传递JSON参数{username:abc@test.com,密码:abctest}我想这是一般性的


如果要将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);
    ...
这可能有帮助:

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

    }

当我点击上面给出的url时,请尝试使用postman客户端查看更多内容。它在broswer中不起作用。请确保它正在运行serviceBasicNameValuePair!=JSONSorry对于延迟回复表示担忧,该链接正在工作,我在AdvanceREST客户端上测试了它,但现在已清除。谢谢你的帮助。
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();

    }