[英]How to save String value that I send from client to server?
我正在做一個將信息從客戶端發送到服務器的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());
}
}
而且,如果您能告訴我如何將其回復給客戶端,那將是很好的。
謝謝。
如果需要將某些內容保存在服務器上,則需要將其放入數據庫中。 這是有關使用Java在Heroku上連接到關系數據庫的文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.