将Json对象发布到Restful Web Service

时间:2017-05-18 14:29:15

标签: java android rest web put

我必须将用户的评论发布到Web服务。但是我失败了。

我有一个AsyncTask类,我在其中执行以下操作:

@Override
    protected JSONObject doInBackground(String... params) {

        JSONObject jsobj = null;
        HttpURLConnection conn = null;

        String urlStr = params[0];
        String message = params[2];
        String name = params[1];

        try {
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.connect();


            jsobj.put("name", name);
            jsobj.put("text", message);
            jsobj.put("id", news_id);

            DataOutputStream writer
                    = new DataOutputStream(conn.getOutputStream());

            writer.writeBytes(jsobj.toString());
            writer.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();


        }


 return jsobj;
    }

在初始化中,我执行以下操作:

DataTask4 tsk = new DataTask4();

tsk.execute("http://94.138.207.51:8080/NewsApp/service/news/savecomment",commentowner,message)

我不知道我做错了什么,但似乎我可以将json对象正确地放到web服务上。

0 个答案:

没有答案
相关问题