2

it my method call

if(guest){
    new JsonTask().execute("URL");
}else{
    new AsyncTaskGetMareker().execute();
}

This is my method:

private class JsonTask extends AsyncTask<String, String, String> {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected String doInBackground(String... params) {


            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();


                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {
                    buffer.append(line+"\n");
                    Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

                }
                return buffer.toString();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (result !=null){

                for (int i =0; i <result.length(); i++){
                    JSONObject jsonObject= null;
                    try {
                        JSONObject json = new JSONObject(result);
                        JSONObject jsonResponse = json.getJSONObject("response");
                        String name = jsonResponse.getString("store_name");
                        String lat = jsonResponse.getString("latitude");
                        String lang=jsonResponse.getString("longitude");
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

This is my error

org.json.JSONException: Value [{"id":1,"store":"дом","category_id":11,"latitude":
2020-02-10 14:24:01.689 13767-13767/? W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)

I found this code on this site and am trying to implement it in my project. I make sure that when checking, either a local or external file is loaded. What i do wrong in my code?Please help me...I don't understand English well, please understand and forgive me

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Urban Side
  • 81
  • 8
  • Which is line 112? One of the ones in onPostExecute()? I don't know those classes well but I'd guess the problem is that your JSON result is actually an array, not an object, but you're trying to parse it as a JSONObject. – Rup Feb 10 '20 at 11:35
  • 2
    As far as I understand, the result that you get by URL is JSON Array not JSON Object – mrgrechkinn Feb 10 '20 at 11:35
  • @mrgrechkinn Most likely, and I can’t figure out how to fix it. – Urban Side Feb 10 '20 at 11:44
  • @UrbanSide in your onPostExecute you should handle result as JSON array; You can find here how to do this https://stackoverflow.com/questions/18977144/how-to-parse-json-array-not-json-object-in-android – mrgrechkinn Feb 10 '20 at 11:47
  • @mrgrechkinn On you url code, i have this [screen](https://sun9-46.userapi.com/c204616/v204616304/62916/cQbBWS5J3TM.jpg) and wery long loading P.sПрошу прощения,вы не русский пользователь часом?) – Urban Side Feb 10 '20 at 11:58
  • 1
    @UrbanSide Could you provide: a full JSON response from your URL ; and share your modified code – mrgrechkinn Feb 10 '20 at 12:02
  • @mrgrechkinn [MyJson](https://pastebin.com/hTqCmPb6) , [My Code](https://pastebin.com/qDXEfYcv) – Urban Side Feb 10 '20 at 12:05
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207527/discussion-between-mrgrechkinn-and-urban-side). – mrgrechkinn Feb 10 '20 at 12:10

1 Answers1

1

The answer that helped me from: mrgrechkinn

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

            if (result !=null){
                JSONArray jsonarray = null;
                try {
                    jsonarray = new JSONArray(result);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonObject= null;
                        try {
                            JSONObject obj = jsonarray.getJSONObject(i);
                            String name = obj.getString("store_name");
                            String lat = obj.getString("latitude");
                            String lang=obj.getString("longitude");
                            String desc=obj.getString("store_desc");
                            String oxr=obj.getString("telephone");
                            String sost=obj.getString("keywords");
                            int cat=obj.getInt("category_id");
                            int id=obj.getInt("id");
                            Log.e("100rad",""+i);
    } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
Urban Side
  • 81
  • 8