如何使用带有请求参数的post方法调用Web服务

时间:2015-05-20 12:22:07

标签: android web-services rest

以请求参数格式集成的其余Web服务就像这样

    {
  "user" : {
    "id" : null,
    "createdBy" : 1,
    "createdOn" : null,
    "emailAddress" : "goza1@apaservices.net",
    "enabled" : "True",
    "firstName" : "Marietnmnnta ",
    "lastName" : "Zarafftgoza",
    "mobileNo" : "556641488346",
    "status" :  null,
    "updatedBy" : null,
    "updatedOn" : null,
    "regType" : "User",
    "profilePicUrl" : "c:",
    "profile" : null
  }
}

如何使用post方法使用此请求调用Web服务,我已在网上检查了很多示例,但请求参数格式我没有得到,有人请帮助

3 个答案:

答案 0 :(得分:0)

只需像这样创建jsonrequest

JSONObject jsonBody = new JSONObject();
jsonBody.put("id", null);
jsonBody.put("createdBy", 1);
jsonBody.put("profile", null);

现在将你的jsonbody添加到主对象

JSONObject jsonMain = new JSONObject();
jsonBody.put("user", jsonBody);

答案 1 :(得分:0)

创建JSONObject以添加输入参数,如下所示,并将其附加到HttpPost对象。

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

String json = "";

JSONObject user = new JSONObject();
user.put("id", null); 
user.put("createdBy", 1); 
user.put("createdOn", null);

json = user.toString();

StringEntity se = new StringEntity(json);

httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

HttpResponse httpResponse = httpclient.execute(httpPost);

答案 2 :(得分:0)

试试这样。我希望它能帮到你..!

JSONObject jsonobj = new JSONObject();  
JSONObject geoJsonObj = new JSONObject();  

try { 

    jsonobj.put("action","put-point");  
    geoJsonObj.put("lng", longitude);  
    geoJsonObj.put("lat", latitude);  
    geoJsonObj.put("rangeKey", rangeKey);  
    geoJsonObj.put("schoolName", "TESTSCHOOL535353");  
    jsonobj.put("request", geoJsonObj);  

} catch (JSONException e) {  
    e.printStackTrace();  
}
new SendData().execute(jsonobj.toString());

public class SendData extends AsyncTask<String, Integer, Double>{  

    String response="";  
    @Override  
    protected Double doInBackground(String... params) {  

        postData(params[0]);  

    }  

    public void postData(String jsondata)  {  

        // Create a new HttpClient and Post Header  
        HttpClient httpclient = new DefaultHttpClient();  

        HttpPost httpPost=new HttpPost("url");  
        try {  
            // Add your data  
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
            nameValuePairs.add(new BasicNameValuePair("json",jsondata));  

            httpPost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));  

            // Execute HTTP Post Request  
            HttpResponse res = httpclient.execute(httpPost);  
            InputStream content = res.getEntity().getContent();  

            BufferedReader buffer = new BufferedReader(new InputStreamReader(content));  
            String s = "";  
            while ((s = buffer.readLine()) != null) {  
                response += s;  
            }  
            System.out.println("response from server"+response);  

        } catch (ClientProtocolException e) {  
             // TODO Auto-generated catch block  
        } catch (IOException e) {  
             // TODO Auto-generated catch block  
        }  
    }  
}  

服务器端 -

protected void doPost(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {  

    String jsondata=request.getParameter("json");  

    //now parse your data from json

    try {
        JSONObject JsonObject=new JSONObject(jsondata);  
        JSONObject object=JsonObject.getJSONObject("request");  
        String action=object.getString("action");  
        String lng=object.getString("lng");  
        String lat=object.getString("lat");  
        String rangeKey=object.getString("rangeKey");  
        String schoolName=object.getString("schoolName");  

    } catch (JSONException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  
} 
相关问题