0
我正在制作一个android应用程序,可以将信息从客户端发送到服务器。我试图从客户端发送JSON对象到服务器端,但我不知道如何将它保存在服务器端,然后再次将其发送回客户端。如何保存我从客户端发送到服务器的字符串值?
我是新来的Heroku服务器和Java EE,所以我不知道如何在服务器端代码。
下面是客户端代码:
public class JsonHeroku {
public void executeGet()
{
new JsonAsyncTask().execute();
}
class JsonAsyncTask extends AsyncTask<Void, Void, String> {
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... params) {
try {
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
url = new URL ("https://shrouded-lake-7996.herokuapp.com/json");
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");
urlConn.setRequestProperty("Host", "android.schoolportal.gr");
urlConn.connect();
//Create JSONObject here
JSONObject jsonParam = new JSONObject();
jsonParam.put("ID", "25");
jsonParam.put("description", "Real");
jsonParam.put("enable", "true");
String str = jsonParam.toString();
byte[] data=str.getBytes("UTF-8");
// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream());
printout.write(data);
printout.flush();
printout.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String content) {
System.out.println("DONE ----------------------------");
System.out.println(content);
}
}
}
现在我必须将其保存在Heroku的服务器:
public class Main {
public static void main(String[] args) {
port(Integer.valueOf(System.getenv("PORT")));
staticFileLocation("/public");
get("/parse", (req, res) -> {
// What do i write here?
return "Hey baby";
});
get("/", (request, response) -> {
Map<String, Object> attributes = new HashMap<>();
attributes.put("message", "Hello World!");
return new ModelAndView(attributes, "index.ftl");
}, new FreeMarkerEngine());
}
}
如果你能告诉我该如何应对它回到客户端会很好。
谢谢。
谢谢,我阅读文档,它是有用的,但我想我会迁移我的代码来解析数据库,因为他们接受JSON作为参数,这将节省我的一些工作。 – MuhammadNe