Android: Http post with parameters not working

前端 未结 4 1521
栀梦
栀梦 2020-12-30 09:40

I need to create an HTTP POST request with parameters. I know there are many examples out there, I have tried using HTTPparams, NameValuePair etc but cant seem to get the co

相关标签:
4条回答
  • 2020-12-30 10:03

    Try this:

    HttpClient client = new DefaultHttpClient();  
        HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login");   
        post.setHeader("Content-type", "application/json");
        post.setHeader("Accept", "application/json");
    JSONObject obj = new JSONObject();
    obj.put("username", "abcd");
    obj.put("password", "1234");
        post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
        HttpResponse response = client.execute(post);  
    
    0 讨论(0)
  • 2020-12-30 10:08

    I'm not quite sure, from your description, but it would seem that your server expects a JSON content object instead of the data being encoded in the URL. Send something like this as the body of your post:

    {"username":"abcd","password":"1234"}
    
    0 讨论(0)
  • 2020-12-30 10:15
    HttpClient client = new DefaultHttpClient();  
    HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login");   
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();  
    
    pairs.add(new BasicNameValuePair("username","abcd"));  
    pairs.add(new BasicNameValuePair("password","1234"));  
    
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8);  
    post.setEntity(entity);  
    HttpResponse response = client.execute(post);  
    

    just try this coz it works perfect for me when i am trying to HTTP post.

    0 讨论(0)
  • 2020-12-30 10:17

    this will probably work for you.

    assuming you already have the json object.

    NOTE: (1)in the server you need to handle th request as utf-8 (also in the DB).

        @SuppressWarnings("unchecked")
    private static HttpResponse executePostRequest(JSONObject jsonData, String url) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(url);
    
        try {
            httpost.setEntity(new ByteArrayEntity(jsonData.toString().getBytes("UTF8")));
            httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json;charset=UTF-8");
            httpost.setHeader("Accept-Charset", "utf-8");
            HttpResponse httpResponse = httpclient.execute(httpost);
            return httpResponse;
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    

    then in the client handle the server response like this:

    String responseBody = EntityUtils
                        .toString(response.getEntity(), "UTF-8");
    
    0 讨论(0)
提交回复
热议问题