简体   繁体   中英

How to send Json String using REST to java webservice in Parameter in android?

Fiends, i sending JSON String with three parameters to java web service method. but on java side method cant print in console. Please guide me what i have to change from below code?

String json = "";
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
        HttpConnectionParams.setSoTimeout(httpParams, 10000);
        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpPost httpPost = new HttpPost(url);
        HttpGet httpGet = new HttpGet(url);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", "ghanshyam");
            jsonObject.put("country", "India");
            jsonObject.put("twitter", "ghahhd");

            json = jsonObject.toString();

            StringEntity se = new StringEntity(json);

            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");

            // 6. set httpPost Entity
            System.out.println(json);

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

            //String s = doGet(url).toString();

            Toast.makeText(getApplicationContext(), "Data Sent", Toast.LENGTH_SHORT).show();

Use the following code to post the json to Java web-service: and get the resopnse as a string.

    JSONObject json = new JSONObject();
    json.put("name", "ghanshyam");
    json.put("country", "India");
    json.put("twitter", "ghahhd");

    HttpPost post = new HttpPost(url);
    post.setHeader("Content-type", "application/json");
    post.setEntity(new StringEntity(json.toString(), "UTF-8"));
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse httpresponse = client.execute(post);
    HttpEntity entity = httpresponse.getEntity();
    InputStream stream = entity.getContent();
    String result = convertStreamToString(stream);

and Your convertStremToString() method will be as follows:

    public static String convertStreamToString(InputStream is)
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try
        {
            while ((line = reader.readLine()) != null)
            {
                    sb.append(line + "\n");
            } 
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return sb.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM