티스토리 뷰
Android/for Class, Project
안드로이드 스튜디오 Android Studio httpURLConnection json reponse string to JSONObject
j0n9m1n1 2018. 11. 11. 21:11반응형
서버에서 json데이터는 잘 받아왔는데 그걸 그대로 JSONObject로 바꾸려니까 화를 냈다ㅠㅠ
org.json.JSONException: Value {~~~~~~~} of type java.lang.String cannot be converted to JSONObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | class GetReport extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... user_info) { String response = ""; try { URL url = new URL("http://123.214.121.100/get_report"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); // 아웃풋 함 conn.setDoInput(true); // 인풋 받음 conn.setRequestMethod("POST"); // 요청 타입 POST conn.setRequestProperty("Accept", "application/text; charset=utf-8"); // 응답 타입 jSON Uri.Builder builder = new Uri.Builder() .appendQueryParameter("lms_id", user_info[0]) .appendQueryParameter("lms_pw", user_info[1]); // .appendQueryParameter("token", user_info[2]); String query = builder.build().getEncodedQuery(); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(query); writer.flush(); writer.close(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { //reponse 200일때 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String inputLine; StringBuffer reply = new StringBuffer(); while ((inputLine = in.readLine()) != null) { reply.append(inputLine); } in.close(); response = reply.toString(); System.out.println("before: " + response); response = response.replaceAll("\\\\", ""); response = response.replace("\"{", "{"); response = response.replace("}\",", "},"); response = response.replace("}\"", "}"); System.out.println("after: " + response); try { JSONObject jsonobject = new JSONObject(response); System.out.println("jsonObject" + jsonobject); } catch (JSONException e) { e.printStackTrace(); } // System.out.println("DATA response = " + response); } else { //response 404, 500등등등 } conn.disconnect(); } catch (MalformedURLException e) { // ... } catch (IOException e) { e.printStackTrace(); // ... } return response; } | cs |
source: https://stackoverflow.com/questions/14009352/org-json-jsonexception-expected-literal-value-at-character-550-of
'Android > for Class, Project' 카테고리의 다른 글
안드로이드 스튜디오 파이어베이스(Firebase) 연동 (0) | 2018.09.14 |
---|
댓글
티스토리 방명록
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
Blog is powered by
Tistory / Designed by
Tistory
Contact: j0n9m1n1@gmail.com
Contact: j0n9m1n1@gmail.com