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

Android 如何使用带有请求参数的post方法调用web服务,android,web-services,rest,Android,Web Services,Rest,以请求参数格式集成的RESTWeb服务如下 { "user" : { "id" : null, "createdBy" : 1, "createdOn" : null, "emailAddress" : "goza1@apaservices.net", "enabled" : "True", "firstName" : "Marietnmnnta ", "lastName" : "Zarafftgoza", "mobil

以请求参数格式集成的RESTWeb服务如下

    {
  "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服务,我在网上查看了这么多示例,但是我没有得到请求参数格式,有人请帮忙创建这样的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);
如下所示,创建用于添加输入参数的JSONObject,并将其附加到HttpPost对象


试试看。我希望它能帮助你

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  
        }  
    }  
}  
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();  
    }  
}