多部分请求的响应正在返回

时间:2015-02-11 07:31:40

标签: android api http-post multipart

我正在尝试将http发布请求作为多部分请求发送到服务器,但是api的响应始终返回为。我已经完成了以下代码,但无法找出它的错误。

String saveGroupUrl = getResources().getString(R.string.base_url)+ getResources().getString(R.string.save_group);
                Log.v("save group url==>", saveGroupUrl);
                HttpPost httppost = new HttpPost(saveGroupUrl);

                HttpContext localContext = new BasicHttpContext();
                MultipartEntity entity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);
                HttpClient httpClient = new DefaultHttpClient();

                try {



                    entity.addPart("device_id",new StringBody(My_Application.androidId));
                    entity.addPart("token_no",new StringBody(prefs.gettokendata()));
                    entity.addPart("user_id",new StringBody(prefs.getuserid()));

                    entity.addPart("group_name", new StringBody(strGrpName));
                    Log.v("group name==>", strGrpName);

                    entity.addPart("short_desc", new StringBody(strDesc));
                    Log.v("group desc==>", strDesc);

                    entity.addPart("description", new StringBody(strLongDesc));
                    Log.v("group long desc==>", strLongDesc);



                    httppost.setEntity(entity);

                    HttpResponse response = httpClient.execute(httppost);
                    HttpEntity resEntity = response.getEntity(); 
                     if (resEntity != null) {    
                           Log.i("RESPONSE",EntityUtils.toString(resEntity));
                     }


                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

1 个答案:

答案 0 :(得分:0)

您没有访问错误的网址。一切正常。然而,

首先,当您访问该网站时,请确保在发出请求之前首次获得响应中存在的Cookie,然后当您必须提出请求时,只需在该请求中输入您的Cookie拿到。

顺便说一句,我建议您使用JSOUPApache HTTP Client而不是本机API。相信我会节省时间和代码行。