0

I am developing an android app.Here,I want to fetch the country name by sending locale means local language.Can anyone tell me where is the problem? I can not fetch the value and displays in spinner. I have done this:-

 public class Main2Activity extends Activity {
    final Context context = this;
    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }} else {
                Log.e("JSON", "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
    private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            return readJSONFeed(urls[0]);
        }
        protected void onPostExecute(String result) {
            Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
            try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                String line;
             while((line=in.readLine())!=null){
                 JSONArray jsonArray = new JSONArray(line);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    List<String> list = new ArrayList<String>();list.add(jsonObject.getString("name"));
                    mySpinner
                            .setAdapter(new ArrayAdapter<String>(
                                    Main2Activity.this,
                                    android.R.layout.simple_spinner_dropdown_item,
                                    list));
                }
             }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = context.getResources().getConfiguration().locale
                .getLanguage();
        url = "http://Its my server address?locale=" + url.toUpperCase();
        Log.e("Web call", url);
                new ReadJSONFeedTask().execute(url);
    }
}

Please help me.

Rami
  • 7,879
  • 12
  • 36
  • 66
user_apr
  • 719
  • 2
  • 10
  • 27
  • what error you are getting? – Piyush Kukadiya Aug 22 '14 at 04:11
  • skipped 48 frames..I think there is a problem in spinner and json array..because my server is getting the request.. – user_apr Aug 22 '14 at 04:17
  • @user3553286 post it. – Rod_Algonquin Aug 22 '14 at 04:18
  • protected void onPostExecute(String result) {} here is the problem..But I cant understand.. – user_apr Aug 22 '14 at 04:19
  • Copy paste your logcat error message rather than manually typing it. And do edit your question and post there – Nabin Aug 22 '14 at 04:19
  • Web call http://myserveraddress/locale=en...choreographer skipped 81 frames..application is doing too much work – user_apr Aug 22 '14 at 04:20
  • Emulator without GPU emulation detected. GC_FOR_ALLOC freed 200K, 9% free 3061K/3340K, paused 45ms, total 45ms GC_FOR_ALLOC freed 146K, 12% free 3104K/3512K, paused 32ms, total 33ms Skipped 35 frames! The application may be doing too much work on its main thread. Attempted to finish an input event but the input event receiver has already been disposed. – user_apr Aug 22 '14 at 04:22
  • 1
    check my answer http://stackoverflow.com/questions/25279661/how-to-fetch-jsondata-in-server-through-android/25280000#25280000 – Deep Shah Aug 22 '14 at 04:28
  • @user3553286 Do you have any problem in posting `LogCat`? – Amit Anand Aug 22 '14 at 04:39

1 Answers1

1

The problem here is the while loop in onPostExecute method. Which is blocking UI thread and will cause ANR. What are you reading from System.in? As per logic I understand that all Country name is available in "result" object. Check what you are getting in result object. You are adding values to result but it is not used. It means logic in doInBackground is useless. I think you are trying to separate lines from result object by reading it from input strem. Instead of this, you can store each line in ArrayList and use it. Below can be a possible solution. Hope This Helps!

public class Main2Activity extends Activity {
        final Context context = this;

        public ArrayList<String> readJSONFeed(String URL) {
            // Add all lines to this list.
            ArrayList<String> data = new ArrayList<String>();
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(URL);
            try {
                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        data.add(line);
                    }
                } else {
                    Log.e("JSON", "Failed to download file");
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return data;
        }

        private class ReadJSONFeedTask extends
                AsyncTask<String, Void, ArrayList<String>> {
            protected ArrayList<String> doInBackground(String... urls) {
                return readJSONFeed(urls[0]);
            }

            protected void onPostExecute(ArrayList<String> result) {
                Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
                try {
                    // Parse each line and add "name" to this list.
                    List<String> nameList = new ArrayList<String>();
                    for (String line : result) {
                        JSONArray jsonArray = new JSONArray(line);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            nameList.add(jsonObject.getString("name"));
                        }
                    }
                    // Once complete traversing is done, set Adapter
                    mySpinner.setAdapter(new ArrayAdapter<String>(
                            Main2Activity.this,
                            android.R.layout.simple_spinner_dropdown_item,
                            nameList));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String url = context.getResources().getConfiguration().locale
                    .getLanguage();
            url = "http://Its my server address?locale=" + url.toUpperCase();
            Log.e("Web call", url);
            new ReadJSONFeedTask().execute(url);
        }
    }
srs
  • 647
  • 3
  • 11
  • from your program I can fetch the country name.now if i want to fetch corresponding dial code when I select a country name..then where I have to add "jsonObject.getString("itu")" as a spinner? my server json the code is like {"name":"Afghanistan","itu":"+93"} – user_apr Aug 22 '14 at 05:27
  • Correct :).. Logic will be similar to "name" – srs Aug 22 '14 at 05:48