Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing logcat: a bit confused here :(
    primarykey
    data
    text
    <p>I'm new to using logcat and debugging in Android. I've analyzing someone else's code which looks like this:</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class RestClient { private static String convertStreamToString(InputStream is) { /* * To convert the InputStream to String we use the BufferedReader.readLine() * method. We iterate until the BufferedReader return null which means * there's no more data to read. Each line will appended to a StringBuilder * and returned as String. */ BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } /* This is a test function which will connects to a given * rest service and prints it's response to Android Log with * labels "Praeda". */ public static void connect(String url) { HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Execute the request HttpResponse response; try { response = httpclient.execute(httpget); // Examine the response status Log.i("Praeda",response.getStatusLine().toString()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release if (entity != null) { // A Simple JSON Response Read InputStream instream = entity.getContent(); String result= convertStreamToString(instream); Log.i("Praeda",result); // A Simple JSONObject Creation JSONObject json=new JSONObject(result); Log.i("Praeda","&lt;jsonobject&gt;\n"+json.toString()+"\n&lt;/jsonobject&gt;"); // A Simple JSONObject Parsing JSONArray nameArray=json.names(); JSONArray valArray=json.toJSONArray(nameArray); for(int i=0;i&lt;valArray.length();i++) { Log.i("Praeda","&lt;jsonname"+i+"&gt;\n"+nameArray.getString(i)+"\n&lt;/jsonname"+i+"&gt;\n" +"&lt;jsonvalue"+i+"&gt;\n"+valArray.getString(i)+"\n&lt;/jsonvalue"+i+"&gt;"); } // A Simple JSONObject Value Pushing json.put("sample key", "sample value"); Log.i("Praeda","&lt;jsonobject&gt;\n"+json.toString()+"\n&lt;/jsonobject&gt;"); // Closing the input stream will trigger connection release instream.close(); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>How do I run this so I can watch the log? Do I need to actually have an activity which calls the function or....? I'm so confused....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload