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

안드로이드 - 서버 통신 질문드립니다.

0 추천
안드로이드와 서버 코드 첨부합니다..비슷한 로직으로는 다른 코드에서는 돌아갔는데 jsonarray를 쓰니 돌아가지 않더라구요. 무엇이 문제인걸까요?

 

public class InsertJson extends AppCompatActivity {

    private static String TAG = "phptest_MainActivity";
    private TextView mTextViewResult;


    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;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_json);

        mTextViewResult = (TextView) findViewById(R.id.textView_main_result);


                InsertData task = new InsertData();
                task.execute("http://211.253.9.84");

    }

    class InsertData extends AsyncTask<String, Void, String>{
        ProgressDialog progressDialog;



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

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


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

            progressDialog.dismiss();
            mTextViewResult.setText(result);
            Log.d(TAG, "POST response  - " + result);
        
        @Override
        protected String doInBackground(String... params) {

            String serverURL = "http://211.253.9.84/deladmission.php";


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

            try {

            URL url = new URL(serverURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();



                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();


                return sb.toString();


            } catch (Exception e) {

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

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

        }
    }

}
jsy (200 포인트) 님이 2017년 8월 23일 질문
안드로이드 모니터입니다!




08-22 15:31:13.789 4707-4707/? I/art: Not late-enabling -Xcheck:jni (already on)
08-22 15:31:13.789 4707-4707/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-22 15:31:13.957 4707-4707/com.example.seuls.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.seuls.myapplication-1/lib/x86
08-22 15:31:13.972 4707-4707/com.example.seuls.myapplication I/InstantRun: starting instant run server: is main process
08-22 15:31:14.203 4707-4707/com.example.seuls.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-22 15:31:14.630 4707-4744/com.example.seuls.myapplication I/System.out: {"group_id":"1","id":"sss"}
08-22 15:31:14.637 4707-4744/com.example.seuls.myapplication E/JH: [{"group_id":"1","id":"sss"},{"group_id":"1","id":"a"}]
08-22 15:31:14.638 4707-4744/com.example.seuls.myapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
                                                                                      [ 08-22 15:31:14.695  4707: 4743 D/         ]
                                                                                      HostConnection::get() New Host Connection established 0xa0739a80, ti                                                                            [ 08-22 15:31:14.698  4707: 4743 W/         ]
                                                                                      Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_dma_v1
08-22 15:31:14.702 4707-4743/com.example.seuls.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
08-22 15:31:14.702 4707-4743/com.example.seuls.myapplication D/OpenGLRenderer: Swap behavior 1
08-22 15:31:14.702 4707-4743/com.example.seuls.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-22 15:31:14.702 4707-4743/com.example.seuls.myapplication D/OpenGLRenderer: Swap behavior 0
08-22 15:31:14.719 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglCreateContext: 0xa3133620: maj 2 min 0 rcv 2
08-22 15:31:14.733 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:14.822 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:14.886 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:14.974 4707-4707/com.example.seuls.myapplication W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
08-22 15:31:14.990 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:15.012 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:15.037 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:15.372 4707-4744/com.example.seuls.myapplication D/phptest_MainActivity: POST response code - 500
08-22 15:31:15.414 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)
08-22 15:31:15.442 4707-4707/com.example.seuls.myapplication D/phptest_MainActivity: POST response  -
08-22 15:31:15.448 4707-4743/com.example.seuls.myapplication D/EGL_emulation: eglMakeCurrent: 0xa3133620: ver 2 0 (tinfo 0x9ea0bc10)

1개의 답변

0 추천

POST response code - 500

로그의 밑에서 4번째 줄에 서버쪽에서 처리받는 부분에서 에러가 있네요. 서버쪽과 주고받기로 한 프로토콜이 맞지 않거나 서버 내부 오류인걸로 보입니다.

minor (13,710 포인트) 님이 2017년 8월 23일 답변
서버코드는 이거입니다. php는 처음이라 잘 몰라서요 ㅠㅠ 뭐가 문제인지 알 수 있을까요?

<?php
$connect = mysql_connect("localhost", "root", "rhxnrpej") or
        die("SQL server에 연결할 수 없습니다.");

mysql_query("SET NAMES UTF8");

//데이터베이스선택

mysql_select_db("register", $connect);

session_start();

$json = $_POST[json];

// parse to php array
$data = json_decode($json , true);

foreach ($data as $user) {
    $id = $user->id;
    $group_id = $user->group_id;


$sql = "delete from admission where id = '$id' and group_id='$group_id'";


$result = mysql_query($sql);

if(!$result){
        die("mysql query error");
}
else {
        echo "delete success"
}
}

?>
<?php
$fp = fopen('vardump.txt', 'w');
fwrite($fp, serialize($myobj));
fclose($fp);
?>
서버쪽에서 json_decode이전에 $_POST[json]을 덤프해보세요
(json_decode에서 문제가 발생한다면 보내는 json 형식이 잘못된겁니다)
...