기본 콘텐츠로 건너뛰기

11월, 2018의 게시물 표시

Android, HttpURLConnection

*** implementation new Thread(new Runnable() { public void run() {     HashMap params = new HashMap();           params.put("param1", "1");     HttpURLConnection connection = null;           try {         URL url = new URL("http://xxxxx.com/api/xxxx");                    connection = (HttpURLConnection)url.openConnection();         connection.setRequestMethod("POST");         connection.setRequestProperty("Accept", "application/json");         connection = ConnectionManager.setParams(connection, params);             connection.connect();             Integer responseCode  = connection.getResponseCode();             if (responseCode == HttpURLConnection.HTTP_OK) {             activity.runOnUiThread(new Runnable(){                 @Override                 public void run() {                     //refresh GUI                 }                     });             }         }catch(Exception e){         e.printStackTrace();       }f

Android, InputStream to JSON

public static JSONObject getInputStreamToJSON (InputStream inputStream) throws Exception { BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream , "UTF-8" )) ; StringBuilder responseStrBuilder = new StringBuilder() ; String inputStr ; while ((inputStr = streamReader.readLine()) != null ) responseStrBuilder.append(inputStr) ; JSONObject jsonObject = new JSONObject(responseStrBuilder.toString()) ; //returns the json object return jsonObject ; }