0

My java code:

  package com.Cmode.ThesisSystem;

  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import android.app.ListActivity;
  import android.net.ParseException;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.Toast;


 public class EventLogs extends ListActivity {

 JSONArray jArray;
 String result = null;
 InputStream is = null;
 StringBuilder sb=null;
 int e_id;
 String e_name;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);


 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();{


 //http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.my.com/eventlogs.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
    Log.e("log_tag", "Error in http connection"+e.toString());

}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
 sb = new StringBuilder();
 sb.append(reader.readLine() + "\n");
 String line="0";
 while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
  }
  is.close();
  result=sb.toString();
  }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
  }

//paring data

  try{
  jArray = new JSONArray(result);
  JSONObject json_data=null;
  for(int i=0;i<jArray.length();i++){
         json_data = jArray.getJSONObject(i);
         e_id=json_data.getInt("id");
         e_name=json_data.getString("event");
     }
  }catch(JSONException e1){
      Toast.makeText(getBaseContext(), "No events found" ,Toast.LENGTH_LONG).show();
  } catch (ParseException e1) {
        e1.printStackTrace();
  }

}
 }

}

I have a problem with this code Error: The application CDroidMonitoring(process.com.Cmode.ThesisSystem) has stopped unexpectedly. Please try again

Im just new to android development. I think I have put something unusable braces or there is something wrong in the code.

ching
  • 41
  • 5
  • Also, when application says it "has stopped unexpectedly" and quits, and exception has happened. See the `logcat` and post here the relevant errors. – Peter Knego Nov 24 '11 at 09:12
  • @PeterKnego 1-24 17:21:09.491: E/AndroidRuntime(713): Uncaught handler: thread main exiting due to uncaught exception – ching Nov 24 '11 at 09:22
  • Yeah that helpa a lot. Which exception and the line of your code that produced it? – Peter Knego Nov 24 '11 at 11:27

1 Answers1

0

Does your application have the INTERNET permission set in the manifest? And for more information about the error check the logcat tab in Eclipse, it usually has a more descriptive error message.

  • There so many errors.. I have also put the internet permission in the manifest.. and still the error is the same – ching Nov 24 '11 at 09:23
  • Did you check the manifest if you have the correct permissions? Also try to find the first exception that occured, it will have the correct reason for the error. – Zsolt Szeberenyi Nov 24 '11 at 09:25
  • 11-24 17:31:08.876: W/dalvikvm(715): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) and 11-24 17:31:08.876: E/AndroidRuntime(715): Uncaught handler: thread main exiting due to uncaught exception – ching Nov 24 '11 at 09:32
  • .. my manifest code – ching Nov 24 '11 at 09:33
  • The permission looks ok, but the exception you wrote is the last one. Find the first one, it will be more helpful. – Zsolt Szeberenyi Nov 24 '11 at 09:38
  • E/AndroidRuntime(715): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Cmode.ThesisSystem/com.Cmode.ThesisSystem.EventLogs}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' – ching Nov 24 '11 at 09:40
  • Check http://stackoverflow.com/questions/3040374/runtime-exception-listview-whose-id-attribute-is-android-r-id-list for your solution. – Zsolt Szeberenyi Nov 24 '11 at 09:48
  • its now ok... but it will not output the data in my table "eventlogs".. it always says "No event found". I think there is something wrong in my code – ching Nov 24 '11 at 10:15