如何从Android应用程序将此类对象发布到Json?

时间:2014-09-15 05:54:25

标签: android json

如何从Android应用程序向Json发送数据到此类对象?

{
"BuId":"",
"CuId":"",
"OrderNumber":"",
"Status":"",
"ODC":{
     "Item":"",
     "ItemType":""
   }
}

我的SERVICEHANDLER如下所示:

public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;


        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);

            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {

            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        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();
        response = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error: " + e.toString());
    }
    return response;`enter code here`
}

帮助我解决这个问题,非常感谢示例代码或教程 先谢谢你。

2 个答案:

答案 0 :(得分:0)

尝试此代码..

创建JSON对象

//Creating Json object 
JSONObject jsonObj = new JSONObject();
try {
    jsonObj.put("BuId", "");
    jsonObj.put("CuId", "");
    jsonObj.put("OrderNumber", "");
    jsonObj.put("Status", "");


    JSONObject child = new JSONObject();
    child .put("Item", "");
    child .put("ItemType", "");

    jsonObj.put("ODC", child);

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

将其发送给服务器

//Sending to server via POST method.. you can use your own

    InputStream inputStream = null;
    String result = "";
    String json = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("URL");

        // convert JSONObject to JSON to String
        json = jsonObj.toString();

        StringEntity se = new StringEntity(json);
        httpPost.setEntity(se);

        // Set some headers to inform server about the type of the content
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if (inputStream != null) {

              BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
              String line = "";
              while((line = bufferedReader.readLine()) != null){
                   result += line; 
              }
             inputStream.close();

        } else {
            result = "Did not work!";
        }
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

参考链接:http://hmkcode.com/android-send-json-data-to-server/

这可能会帮助你...

答案 1 :(得分:-1)

HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            try {
                HttpResponse response = client.execute(post);
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);

                }
                String data = sb.toString();
                // Log.d("data","All dta is "+data);

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