How to send POST request in JSON using HTTPClient in Android?

后端 未结 5 1685
别跟我提以往
别跟我提以往 2020-11-22 07:49

I\'m trying to figure out how to POST JSON from Android by using HTTPClient. I\'ve been trying to figure this out for a while, I have found plenty of examples online, but I

5条回答
  •  误落风尘
    2020-11-22 08:38

    There are couple of ways to establish HHTP connection and fetch data from a RESTFULL web service. The most recent one is GSON. But before you proceed to GSON you must have some idea of the most traditional way of creating an HTTP Client and perform data communication with a remote server. I have mentioned both the methods to send POST & GET requests using HTTPClient.

    /**
     * This method is used to process GET requests to the server.
     * 
     * @param url 
     * @return String
     * @throws IOException
     */
    public static String connect(String url) throws IOException {
    
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 60*1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 60*1000;
    
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        try {
    
            response = httpclient.execute(httpget);
    
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                //instream.close();
            }
        } 
        catch (ClientProtocolException e) {
            Utilities.showDLog("connect","ClientProtocolException:-"+e);
        } catch (IOException e) {
            Utilities.showDLog("connect","IOException:-"+e); 
        }
        return result;
    }
    
    
     /**
     * This method is used to send POST requests to the server.
     * 
     * @param URL
     * @param paramenter
     * @return result of server response
     */
    static public String postHTPPRequest(String URL, String paramenter) {       
    
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 60*1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 60*1000;
    
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httppost = new HttpPost(URL);
        httppost.setHeader("Content-Type", "application/json");
        try {
            if (paramenter != null) {
                StringEntity tmp = null;
                tmp = new StringEntity(paramenter, "UTF-8");
                httppost.setEntity(tmp);
            }
            HttpResponse httpResponse = null;
            httpResponse = httpclient.execute(httppost);
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                InputStream input = null;
                input = entity.getContent();
                String res = convertStreamToString(input);
                return res;
            }
        } 
         catch (Exception e) {
            System.out.print(e.toString());
        }
        return null;
    }
    

提交回复
热议问题