将JSON数据发布到Android中的webservice

时间:2013-04-08 06:05:31

标签: android json

我在应该做一个相当简单的任务时遇到一些问题。我只需要一个JSON数组,其中包含一个JSON对象即可发布到我的webservice。整个URL请求需要格式如下:

http://www.myserver.com/myservice.php?location_data= [{ “KEY1”: “VAL1”, “KEY2”: “val2的” ....}]

2 个答案:

答案 0 :(得分:3)

try {

            HttpClient httpclient = DefaultHttpClient();

            HttpGet httpget = new HttpGet(URL+"?location_data="+JSONARRAY);

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();


        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

答案 1 :(得分:0)

Orginally from here

 HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(
        "http://www.myserver.com/myservice.php"
        );

httpPost.setHeader("content-type", "application/json");



JSONObject locationData = new JSONObject();

locationData .put("key1", "val1");
locationData .put("key2", "val2");


StringEntity entity = new StringEntity(locationData.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);