-
클라이언트 측인 Android환경에서는 Volley를 사용한다.
서버측 flask에서는
import flask app = flask.Flask(__name__) @app.route("/test1") def rt_test1(): return flask.jsonify({'tk':'tokentest1'}), 200 app.run(host='0.0.0.0')
Android에서는 volley.Response.Listener을 이용해 응답 왔을 때 처리를 등록 한다.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Response.Listener<String> rplsn = new Response.Listener<String>() { @Override public void onResponse(String response) { ((TextView)findViewById(R.id.textview1)).setText(response.toString()); if(false) try { JSONObject jo = new JSONObject(response); ((TextView)findViewById(R.id.textview2)).setText(jo.getString("tk")); } catch (JSONException e) { Toast.makeText(getApplicationContext(), "Err onResponseJson: " + e.toString(), Toast.LENGTH_LONG).show(); } } }; Response.ErrorListener errlsn = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "Err ErrorListener: " + error.toString(), Toast.LENGTH_SHORT).show(); } }; String url = "192.168.0.11"; StringRequest req = new StringRequest(Request.Method.GET, url, rplsn, errlsn); //req.setShouldCache(false); RequestQueue rq = Volley.newRequestQueue(MainActivity.this); //rq.getCache().clear(); rq.add(req); }
'Python' 카테고리의 다른 글
Flask render_template (0) 2022.07.31 가변 파라미터 (0) 2022.07.23 컴프리헨션(Comprehension) (0) 2022.07.23 Class (클래스) (0) 2021.11.11 Python popen (0) 2021.06.23 Python Sandbox (0) 2021.06.23 Opencv 윤곽 검출 (0) 2021.04.12 Opencv boundingRect (0) 2021.04.12