sitelink1 https://github.com/bho7982/JSON/blob/mas...ivity.java 
sitelink2  
sitelink3  

아래의 AsyncTask를 상속받아 오버라이딩하는 클래스에서 오류가 나므로 위에 추가한 게시물의 코드를 참고하도록 하자 (테스트 환경 : AndroidStudio3.2.1, JRE1.8.0, OpenJDK64bit)

하기의 코드는 구현 방안에 대해서만 참고하고 실제 샘플코드로는 활용하지 않는다

 

 

package com.example.ddil.myapplication;

import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

    TextView txt_ID, txt_pwd, txt_name, txt_birthday;
    Button btn_id_check, btn_submit;
    EditText edt_ID, edt_pwd1, edt_pwd2, edt_frist_name, edt_last_name, edt_birthday;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //textview define
        txt_ID = (TextView) findViewById(R.id.txt_ID);
        txt_pwd = (TextView) findViewById(R.id.txt_pwd);
        txt_name = (TextView) findViewById(R.id.txt_name);
        txt_birthday = (TextView) findViewById(R.id.txt_birthday);

        //button define
        btn_submit = (Button) findViewById(R.id.btn_submit);

        //edittext define
        edt_ID = (EditText) findViewById(R.id.edt_ID);
        edt_pwd1 = (EditText) findViewById(R.id.edt_pwd1);
        edt_pwd2 = (EditText) findViewById(R.id.edt_pwd2);
        edt_frist_name = (EditText) findViewById(R.id.edt_frist_name);
        edt_last_name = (EditText) findViewById(R.id.edt_last_name);
        edt_birthday = (EditText) findViewById(R.id.edt_birthday);


        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //JsonTransfer는 Json데이터를 전송하는것. (아래에서 JsonTransfer라는 함수를 정의한다.)
                JsonTransfer data_transfer = new JsonTransfer();

                //JSONObject는 JSON을 만들기 위함.
                JSONObject json_dataTransfer = new JSONObject();
                try
                {
                    //json_dataTransfer에 ("키값" : "보낼데이터") 형식으로 저장한다.
                    json_dataTransfer.put("user_id", edt_ID.getText().toString());
                    json_dataTransfer.put("user_password", edt_pwd1.getText().toString());
                    json_dataTransfer.put("first_name", edt_frist_name.getText().toString());
                    json_dataTransfer.put("last_name", edt_last_name.getText().toString());
                    json_dataTransfer.put("user_birthday", edt_birthday.getText().toString());

                    //json_dataTransfer의 데이터들을 하나의 json_string으로 묶는다.
                    String json_string = json_dataTransfer.toString();

                    //보내야 할 곳 주소 정의
                    String url = "보내야할곳 주소!";

                    //보내기 전에 json_string 양 쪽 끝에 대괄호를 붙인다. (Object로 처리하기 때문이다. 만약 Array로 처리한다면, 대괄호는 필요없다고 한다.)
                    data_transfer.execute(url,"["+json_string+"]");

                    //Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG);
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public class JsonTransfer extends AsyncTask{
        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection urlConnection;
            String data = params[1];
            String result = null;
            try {
                //Connect
                urlConnection = (HttpURLConnection) ((new URL(params[0]).openConnection()));
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                urlConnection.setReadTimeout(10000 /*milliseconds*/);
                urlConnection.setConnectTimeout(15000 /* milliseconds */);
                urlConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                urlConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
                //urlConnection.setRequestProperty("Content-Type", "application/json");
                //urlConnection.setRequestProperty("Accept", "application/json");
                urlConnection.setRequestMethod("POST");
                // urlConnection.setFixedLengthStreamingMode(data.getBytes().length);
                //uid,mac,filename,time

                //Write
                OutputStream outputStream = urlConnection.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                writer.write(data);
                writer.close();
                outputStream.close();

                //Read
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

                String line = null;
                StringBuilder sb = new StringBuilder();


                while ((line = bufferedReader.readLine()) != null)
                {
                    sb.append(line);
                }

                bufferedReader.close();
                result = sb.toString();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    }
}

번호 제목 글쓴이 날짜 조회 수
68 TTS 를 위한 스마트폰 설정 및 TTS 샘플 file 황제낙엽 2019.02.16 460
67 Creating swipe views with tabs file 황제낙엽 2019.02.10 102
66 [번역] 안드로이드 ViewPager 를 이용한 수평 화면 전환 file 황제낙엽 2019.02.09 71
65 동적 레이아웃 생성과 자동 줄바꿈 구현 file 황제낙엽 2018.12.26 311
64 qemu-system-~.exe 의 작동이 중지되었습니다 file 황제낙엽 2018.11.27 55
63 Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. file 황제낙엽 2018.11.27 59
62 Error:Minimum supported Gradle version is 4.1. 황제낙엽 2018.11.27 84
61 No toolchains found in the NDK toolchains folder for ABI 황제낙엽 2018.11.27 33
60 [성공샘플] HttpURLConnection 을 이용하여 JSON 데이터 보내기 예제 황제낙엽 2018.11.10 649
59 [Android] 네이버 음성합성(TTS) API 사용해 보기 file 황제낙엽 2018.11.01 167
58 [Android] TTS (Text To Speech) API 샘플 코드 file 황제낙엽 2018.11.01 128
57 Google Cloud API 설정법 file 황제낙엽 2018.11.01 39
56 AsyncTask 사용하기 황제낙엽 2018.10.29 36
55 Volley 소개 및 관련 링크 황제낙엽 2018.10.29 63
54 AsyncTask 를 이용한 HttpURLConnection 사용법 [1] 황제낙엽 2018.10.20 33
» HttpURLConnection 을 이용하여 JSON 데이터 보내기 예제 [1] file 황제낙엽 2018.10.20 102
52 STT 학습 링크 모음 (sample link) 황제낙엽 2018.10.11 552
51 코틀린(Kotlin) 학습용 링크 모음 황제낙엽 2018.10.11 64
50 저장소 파일 불러올 때 권한 요청 설정 file 황제낙엽 2018.08.21 55
49 안드로이드 파일시스템에 파일 생성하여 데이터 저장, 불러오기 예제 황제낙엽 2018.08.21 58