Java 将Json对象发布到Restful Web服务
我必须将用户的评论发布到web服务。然而我失败了 我有一个AsyncTask类,我在其中执行以下操作:Java 将Json对象发布到Restful Web服务,java,android,rest,web,put,Java,Android,Rest,Web,Put,我必须将用户的评论发布到web服务。然而我失败了 我有一个AsyncTask类,我在其中执行以下操作: @Override protected JSONObject doInBackground(String... params) { JSONObject jsobj = null; HttpURLConnection conn = null; String urlStr = params[0]; String mess
@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服务中 到底什么失败了?您能给出一条错误消息或堆栈跟踪吗?它保持进度条的下载,但不会将任何内容放入服务器。我看到您初始化了一个名为jsonobj的JSONObject,但您没有填充它(因为您填充了另一个)是的,我已经修复了,谢谢你,但是现在应用程序崩溃了…尝试使用改装库-这会让你的生活更简单-不再需要搞乱连接和JSONObject