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

급한 ..JsonException 질문 드리겠습니다..

0 추천
 

서버에서 데이터를 받지 못하는건지... 아니면 받는데 josn형식으로 변환이 안되었다고 하는데... 어디가 에러인지 감이 잡히지 않네요... 급해서.. 도움좀 빨리 부탁드려도 될런지요..!

//여기서부터 서버 연동 소스
    private class GetData extends AsyncTask<String, Void, String> {
        ProgressDialog progressDialog;
        String errorString = null;

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

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


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

            progressDialog.dismiss();
        //    mTextViewResult.setText(result);
            Log.d(TAG, "response  - " + result);

            if (result == null){

            //    mTextViewResult.setText(errorString);
            }
            else {

                mJsonString = result;
                showResult();
            }
        }


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

            String serverURL = params[0];


            try {

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


                httpURLConnection.setReadTimeout(5000);
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.connect();


                int responseStatusCode = httpURLConnection.getResponseCode();
                Log.d(TAG, "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;

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


                bufferedReader.close();


                return sb.toString().trim();


            } catch (Exception e) {

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

                return null;
            }

        }
    }


    private void showResult(){
        Log.d(TAG, "ddddd");
        Log.d(TAG, mJsonString);
        try {
            JSONObject jsonObject = new JSONObject(mJsonString);
           // jsonObject.optJSONArray(mJsonString);

            JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject item = jsonArray.getJSONObject(i);

                String num = item.getString(NUM);
                String location = item.getString(LOCATION);
                String name1 = item.getString(NAME1);
                String a1 = item.getString(mA1);
                String flag1 = item.getString(FLAG1);
                String name2 = item.getString(NAME2);
                String a2 = item.getString(mA2);
                String flag2 = item.getString(FLAG2);

                Log.d(TAG, "여기까지옴");
                hashMap = new HashMap<>();
                hashMap.put("Content-Type", "application/json; charset=utf-8");
                hashMap.put(NUM, num);
                hashMap.put(LOCATION, location);
                hashMap.put(NAME1, name1);
                hashMap.put(mA1, a1);
                hashMap.put(FLAG1, flag1);
                hashMap.put(NAME2, name2);
                hashMap.put(mA2, a2);
                hashMap.put(FLAG2, flag2);
                Log.d(TAG, "여기까지옴2");

                Log.d(TAG, "num => "+hashMap.get("num").toString());
                Log.d(TAG, "location => "+hashMap.get("name2").toString());
                Log.d(TAG, "flag2 => "+hashMap.get("flag2").toString());
                Log.d(TAG, "flag1 => "+hashMap.get("flag1").toString());
                int m1 = Integer.parseInt(hashMap.get("mA1").toString());
                int m2 = Integer.parseInt(hashMap.get("mA2").toString());
                machValueTxt1.setText(m1 + " W");
                machValueTxt2.setText(m2 + " W");
                totalUseValueTxt2.setText(m1+m2 + " W");

            }



        } catch (JSONException e) {

            Log.d(TAG, "showResult : ", e);
        }

    }
}

 

LEE 님이 2018년 6월 14일 질문
06-14 07:47:35.982 16022-16040/com.example.home.multitabapp D/EGL_emulation: eglMakeCurrent: 0x991152a0: ver 2 0 (tinfo 0x98f18bc0)
06-14 07:47:37.203 16022-16046/com.example.home.multitabapp D/phptest_MainActivity: response code - 404
06-14 07:47:37.258 16022-16040/com.example.home.multitabapp D/EGL_emulation: eglMakeCurrent: 0x991152a0: ver 2 0 (tinfo 0x98f18bc0)
06-14 07:47:37.344 16022-16040/com.example.home.multitabapp D/EGL_emulation: eglMakeCurrent: 0x991152a0: ver 2 0 (tinfo 0x98f18bc0)
06-14 07:47:37.364 16022-16022/com.example.home.multitabapp D/phptest_MainActivity: response  - <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL /getjson.php was not found on this server.</p><hr><address>Apache/2.4.25 (Raspbian) Server at 192.168.0.12 Port 80</address></body></html>
06-14 07:47:37.364 16022-16022/com.example.home.multitabapp D/phptest_MainActivity: ddddd
06-14 07:47:37.364 16022-16022/com.example.home.multitabapp D/phptest_MainActivity: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL /getjson.php was not found on this server.</p><hr><address>Apache/2.4.25 (Raspbian) Server at 192.168.0.12 Port 80</address></body></html>
06-14 07:47:37.365 16022-16022/com.example.home.multitabapp D/phptest_MainActivity: showResult :
                                                                                    org.json.JSONException: Value webnautes of type java.lang.String cannot be converted to JSONObject
                                                                                        at org.json.JSON.typeMismatch(JSON.java:111)
                                                                                        at org.json.JSONObject.<init>(JSONObject.java:163)
                                                                                        at org.json.JSONObject.<init>(JSONObject.java:176)
                                                                                        at com.example.home.multitabapp.MultitapActivity.showResult(MultitapActivity.java:362)
                                                                                        at com.example.home.multitabapp.MultitapActivity.access$200(MultitapActivity.java:49)
                                                                                        at com.example.home.multitabapp.MultitapActivity$GetData.onPostExecute(MultitapActivity.java:295)
                                                                                        at com.example.home.multitabapp.MultitapActivity$GetData.onPostExecute(MultitapActivity.java:267)
                                                                                        at android.os.AsyncTask.finish(AsyncTask.java:695)
                                                                                        at android.os.AsyncTask.-wrap1(Unknown Source:0)
                                                                                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

로그캣입니다..!

1개의 답변

0 추천

로그 메시지를 확인하세요 => The requested URL /getjson.php was not found on this server

aucd29 (218,390 포인트) 님이 2018년 6월 15일 답변
...