有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何向服务器发送json

我有一个json,将在“Post”中发送到服务器。 json中有3个图像的参数,可以使用multipart上传到服务器上。我没有额外的参数将其发送到多个部分

当我尝试使用chrome的“邮递员”应用程序来测试它时

这里有两个案例-

案例1:-在body&;中选择“原始”选项;在POSTMAN中设置内容类型“application json”

然后,所有数据都已发送到服务器,但图像除外

enter image description here

案例2:选择邮递员中的“表单数据”选项

我只需发送Json的一部分,如图像、证书、地点、地址、联系人、成就、级别、在家、经过认证,但无法通过“coach\u free\u batch”、“coach\u session”和;json的“体育”部分

for case 2

请提供在服务器上上载所有字段的解决方案

第二种情况下由邮递员发送“运动”、“免费教练会议”的想法或任何其他想法

Json

{“coach_free_batch”:[{“sport_id”:“10”,“batch_time”:“end_time”:“1:51 PM”,“start_time”:“12:51 PM”},{“end_time”:“11:51 AM”,“start_time”:“10:51 AM”},{“end_time”:“1:51 PM”,“start_time”:“11:51 AM”}, {“运动id”:“4”,“批量计时”:[{“结束时间”:“下午1:52”,“开始时间”:“下午12:52”}, {“结束时间”:“下午3:52”,“开始时间”:“下午2:52”},{“结束时间”:“下午3:54”,“开始时间”:“下午2:52”}], “coach_session”:[{“计时”:“2小时”,“速率”:“24”},{“计时”:“1小时”,“速率”:“40”}, {“计时”:“3小时”,“速率”:“70”}], “成就”:“国家级”, “级别”:“竞争”,“用户令牌”:“XJnQCAz1ssuUCjgHtFs6”,“在家”:“1”, “sports”:[{“user\u sports\u label\u id”:“7”,“sport\u id”:“2”},{“user\u sports\u label\u id”:“10”,“sport\u id”:“3”}, {“用户体育标识”:“3”,“体育标识”:“4”}, “图像”:[{“图像”:“xxx”}, {“image”:“xxx”}], “经验”:“2年以上”,“地点”:“地址”,“联系人”:“8236968542”, “证书”:[{“证书”:“证书1”},{“证书”:“证书2”},{“证书”:“证书3”}]}

将数据作为原始数据发送的方法

public static String sendDataInJSONFormat(String url, String json) {
    
    // initialize
    InputStream is = null;
    String result = null;
    String url1 = "";
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setHeader("Content-Type",
                "multipart/form-data; application/json; charset=UTF-8;text/plain");

        HttpParams httpParameters = new BasicHttpParams();
        int timeoutSocket = 25000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        
        StringEntity se = new StringEntity(json.toString(), "UTF-8");
        httppost.setEntity(se);
        
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
        e.printStackTrace();
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
        e.printStackTrace();
    }

    return result;
}

共 (0) 个答案