AMS91
AMS91

Reputation: 175

Displaying a JSON Google Search in The LogCat

I'm trying to get the search result from the URL

https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON

and displaying it into the LogCat using log.v().

The problem is that instead of displaying the search results in the LogCat, the program keeps running non stop and what I see in the LogCat is an endless list of this

11-19 15:51:11.256: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 8ms, total 8ms

11-19 15:51:11.264: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 8ms, total 8ms

11-19 15:51:11.272: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 5ms, total 5ms

11-19 15:51:11.272: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 4ms, total 5ms

11-19 15:51:11.324: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 45ms, total 45ms

11-19 15:51:11.332: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 8ms, total 8ms

11-19 15:51:11.400: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 23ms, total 23ms

11-19 15:51:11.424: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 2ms, total 2ms

11-19 15:51:11.464: D/dalvikvm(1251): GC_FOR_ALLOC freed 480K, 14% free 6651K/7688K, paused 26ms, total 26ms

This never stops and keeps going on and on until I kill the app in the emulator.

Here is my code

public class MainActivity extends Activity {

public static final String TAG= MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if(isNetworkAvilable()){
        GetSearchData getSearchData = new GetSearchData();
        getSearchData.execute();
        
    }
    else{
        Toast.makeText(this, "The network is down", Toast.LENGTH_SHORT).show();
    }
    
}


private boolean isNetworkAvilable() {
    // TODO Auto-generated method stub
    
    boolean isAvailable= false;
    ConnectivityManager manager=  (ConnectivityManager) 
            getSystemService(CONNECTIVITY_SERVICE);
    
    NetworkInfo networkInfo=manager.getActiveNetworkInfo();
    
    if(networkInfo !=null && networkInfo.isConnected()){
        
        isAvailable=true;
    }
    
    return isAvailable;
}




private class GetSearchData extends AsyncTask<Object, Void, String>{
    
    protected String doInBackground(Object... params){
        
        int responseCode=-1;
        try{
            
            URL url=new URL("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON");
            
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            
            connection.connect();
            
             responseCode= connection.getResponseCode();
             
            // Log.i(TAG, "Code: "+responseCode);
            
             if(responseCode== HttpURLConnection.HTTP_OK){
                 InputStream inputStream= connection.getInputStream();
                 Reader reader= new InputStreamReader(inputStream);
                 
                 int nextCharacter;
                 String responseData="";
                 while(true){
                     nextCharacter=reader.read();
                     
                     
                     
                     if(responseCode==-1){
                         break;
                     }
                     
                     responseData+= (char) nextCharacter;
                     
                 }
                 
                 JSONObject jsonObject= new JSONObject(responseData);
                 
                 
                 
                JSONObject jsonObject2= jsonObject.getJSONObject("responseData");
                 
                // JSONObject jsonObject3= jsonObject.getJSONObject("results");
                 
                 JSONArray jsonArray= jsonObject2.getJSONArray("results");
                 
                 
                 for(int i=0; i<jsonArray.length(); i++){
                     JSONObject results = jsonArray.getJSONObject(i);
                     
                     String title= results.getString("title");
                     
                     Log.v(TAG, "Result "+i+" : "+title);
                     
                 }
                 
                 
             }
             else{
                 Log.i(TAG, "Wrong Response Code: " + responseCode);
             }
             
            
            
        }
        catch(MalformedURLException e){
            Log.e(TAG, "Exception: "+e);
        }
        catch(IOException e){
            Log.e(TAG, "Exception: "+e);
        }
        
        catch(Exception e){
            Log.e(TAG, "Exception: "+e);
        }
        
        return "Code: "+responseCode;
    }
    
    
    
}

}

Upvotes: 0

Views: 64

Answers (1)

RogueBaneling
RogueBaneling

Reputation: 4471

Try this code for making your network call, it should work properly for at least getting the data from the internet and then posting it in the LogCat.

public class GetSearchData extends AsyncTask<Void,Void,Void> {

        @Override
        protected Void doInBackground(Void... params) {
            String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON";
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try{
                HttpResponse httpResponse = httpClient.execute(httpGet);
                String responseData = inputStreamToString(httpResponse.getEntity().getContent()).toString();
                Log.v("OUTPUT", responseData);

                JSONObject jsonObject= new JSONObject(responseData);
                JSONObject jsonObject2= jsonObject.getJSONObject("responseData");
                JSONArray jsonArray= jsonObject2.getJSONArray("results");

                for (int i = 0; i < jsonArray.length(); i++){
                   JSONObject results = jsonArray.getJSONObject(i);
                   String title = results.getString("title");
                   Log.v(TAG, "Result "+i+" : "+title);
                }

            } catch (IOException e){
                e.printStackTrace();
            } catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }

        public static StringBuilder inputStreamToString(InputStream is){
            String line;
            StringBuilder sb = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            try {
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sb;
        }
    }

Upvotes: 0

Related Questions