마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 php jsonarray를 통한 통신에 관해 질문이요! (코드 약간 수정)

0 추천
php코드가 안돌아가더라구요 ㅠㅠ 이게 안드로이드에서 post로 잘 보내지지 않아서 그런것같은데..
어디서 오류가 나는걸까요?
 
 
코드입니다!
public class MainActivity extends AppCompatActivity {
 
<pre>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);

Button button2 = (Button) findViewById(R.id.button2); 
// button2를 누르면 InsertData4에 맞는 task.execute("서버주소")가 실행됩니다.


}
    public static String makeJsonData() {
        JsonObject jsonObject1 = new JsonObject();
        jsonObject1.addProperty("group_id", "1");
        jsonObject1.addProperty("id", "sss");

        JsonObject jsonObject2 = new JsonObject();
        jsonObject2.addProperty("group_id", "1");
        jsonObject2.addProperty("id", "a");

        String str = jsonObject1.toString();
        System.out.println(str);

        JsonArray jArray = new JsonArray();

        jArray.add(jsonObject1);
        jArray.add(jsonObject2);

        String str1 = jArray.toString();
        Log.e("JH", str1);
        return str1;
    }
class InsertData4 extends AsyncTask<String, Void, String> {
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = ProgressDialog.show(MainActivity.this,
                "Please Wait", null, true, true);

    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        progressDialog.dismiss();

        Log.d(TAG, "POST response  - " + result);
    }


    @Override
    protected String doInBackground(String... params) {

        try {


            String serverURL = "http://211.253.9.84/deladmission.php";
            URL url = new URL(serverURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();


            // JSONArray형태로 데이터를 만든다.
            // JSONArray를 String 문자열로 변환한다.
            String json = makeJsonData();
            String postParameters = "json=" + json;


            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setRequestMethod("POST");
            //httpURLConnection.setRequestProperty("content-type", "application/json");
            httpURLConnection.setDoInput(true);
            httpURLConnection.connect();


            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(postParameters.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();


            int responseStatusCode = httpURLConnection.getResponseCode();
            Log.d(TAG, "POST response code - " + responseStatusCode);


            InputStream inputStream;
            if (responseStatusCode == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
            } else {
                inputStream = httpURLConnection.getErrorStream();
            }

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

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

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



            bufferedReader.close();

            inputStream.close();

            return sb.toString();


        } catch (Exception e) {

            Log.d(TAG, "InsertData: Error ", e);

            return new String("Error: " + e.getMessage());
        }

    }
}
jsy (200 포인트) 님이 2017년 8월 22일 질문
jsy님이 2017년 8월 22일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...