Android GET and POST Request

前端 未结 6 1716
执念已碎
执念已碎 2021-02-01 11:14

Can anyone point me to a good implementation of a way to send GET and POST Requests. They are alot of ways to do these, and i am looking for the best implementation. Secondly is

6条回答
  •  走了就别回头了
    2021-02-01 11:59

     protected String doInBackground(String... strings) {
            String response = null;
            String data = null;
            try {
                data = URLEncoder.encode("CustomerEmail", "UTF-8")
                        + "=" + URLEncoder.encode(username, "UTF-8");
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String url = Constant.URL_FORGOT_PASSWORD;// this is url 
            response = ServiceHandler.postData(url,data);
            if (response.equals("")){
                return response;
            }else {
                return response;
            }
    
        }
    
    
    public static String postData(String urlpath,String data){
    
        String text = "";
        BufferedReader reader=null;
        try
        {
            // Defined URL  where to send data
            URL url = new URL(urlpath);
    
            // Send POST data request
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write( data );
            wr.flush();
    
            // Get the server response
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
    
            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            text = sb.toString();
            return text;
        }
        catch(Exception ex)
        {
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch(Exception ex) {}
        }
        return text;
    }
    

提交回复
热议问题