HttpPost in android not working

吃可爱长大的小学妹 提交于 2019-12-12 03:56:14

问题


I am trying to connect to apache tomcat server using HTTP POST, when i see LOG file of server it showing GET /login/validate_doc.jsp HTTP/1.1" 200 685 ,

which means it is getting a GET request when i am sending using HttpPost and form parameters are not received by server.

my code is below:

        HttpPost post_http=null;
        post_http=new HttpPost("http://somexx.ac.in/medONmob/validate_doc.jsp");
        try 
        {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("username",username));
            nameValuePairs.add(new BasicNameValuePair("password",password));

            post_http.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

Where am i wrong ...??? Help me out please


回答1:


Try specifying encoding when constructing UrlEncodedFormEntity. By default it is ISO-8859-1. Also this will make your code future safe

Creating a UrlEncodedFormEntity from a List of NameValuePairs throws a NullPointerException




回答2:


Given that you are using a post, then you probably are sending data on your request body, Am I right?, then you have to specify the content-type of the data you are sending in the headers, in order to execute a proper post:). For example if I am sending a json in the request body then I should add a header like this:

request.addHeader("content-type", "text/json");

Cheers



来源:https://stackoverflow.com/questions/16371016/httppost-in-android-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!