How to use OKHTTP to make a post request?

后端 未结 13 1073
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 05:06

I read some examples which are posting jsons to the server.

some one says :

OkHttp is an implementation of the HttpUrlConnection interface p

13条回答
  •  囚心锁ツ
    2020-12-02 05:40

     public static JSONObject doPostRequestWithSingleFile(String url,HashMap data, File file,String fileParam) {
    
            try {
                final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    
                RequestBody requestBody;
                MultipartBuilder mBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
    
                for (String key : data.keySet()) {
                    String value = data.get(key);
                    Utility.printLog("Key Values", key + "-----------------" + value);
    
                    mBuilder.addFormDataPart(key, value);
    
                }
                if(file!=null) {
                    Log.e("File Name", file.getName() + "===========");
                    if (file.exists()) {
                        mBuilder.addFormDataPart(fileParam, file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
                    }
                }
                requestBody = mBuilder.build();
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
    
                OkHttpClient client = new OkHttpClient();
                Response response = client.newCall(request).execute();
                String result=response.body().string();
                Utility.printLog("Response",result+"");
                return new JSONObject(result);
    
            } catch (UnknownHostException | UnsupportedEncodingException e) {
                Log.e(TAG, "Error: " + e.getLocalizedMessage());
                JSONObject jsonObject=new JSONObject();
    
                try {
                    jsonObject.put("status","false");
                    jsonObject.put("message",e.getLocalizedMessage());
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            } catch (Exception e) {
                Log.e(TAG, "Other Error: " + e.getMessage());
                JSONObject jsonObject=new JSONObject();
    
                try {
                    jsonObject.put("status","false");
                    jsonObject.put("message",e.getLocalizedMessage());
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
            return null;
        }
        public static JSONObject doGetRequest(HashMap param,String url) {
            JSONObject result = null;
            String response;
            Set keys = param.keySet();
    
            int count = 0;
            for (Iterator i = keys.iterator(); i.hasNext(); ) {
                count++;
                String key = (String) i.next();
                String value = (String) param.get(key);
                if (count == param.size()) {
                    Log.e("Key",key+"");
                    Log.e("Value",value+"");
                    url += key + "=" + URLEncoder.encode(value);
    
                } else {
                    Log.e("Key",key+"");
                    Log.e("Value",value+"");
    
                    url += key + "=" + URLEncoder.encode(value) + "&";
                }
    
            }
    /*
            try {
                url=  URLEncoder.encode(url, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }*/
            Log.e("URL", url);
            OkHttpClient client = new OkHttpClient();
    
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response responseClient = null;
            try {
    
    
                responseClient = client.newCall(request).execute();
                response = responseClient.body().string();
                result = new JSONObject(response);
                Log.e("response", response+"==============");
            } catch (Exception e) {
                JSONObject jsonObject=new JSONObject();
    
                try {
                    jsonObject.put("status","false");
                    jsonObject.put("message",e.getLocalizedMessage());
                    return  jsonObject;
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
    
            return result;
    
        }
    

提交回复
热议问题