我的代码没有错误,但是当我运行android应用程序时,logcats显示:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.testes/com.testes.MainActivity}: java.lang.RuntimeException: Unable to
start activity ComponentInfo{com.testes/com.testes.View_data}:
android.os.NetworkOnMainThreadException
View_data.java:
public class View_data extends ListActivity {
private ArrayList<HashMap<String, String>> daftar_sektor = new
ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
this.getDataMHS(); //memanggil method getDataMHS (ada di bawah)
}
public void getDataMHS(){
final String url = "http://10.0.2.2/android/check_sektor.php";
Map<String, String> params= new HashMap<String, String>();
HttpRequestApi http = new HttpRequestApi(url, params,
HttpRequestApi.Method.GET);
String response = http.sendRequest();
try {
JSONArray jsarray= new JSONArray(response);
for (int i=0;i<jsarray.length();i++){
JSONObject jso = (JSONObject) jsarray.get(i);
HashMap<String, String> mapMHS = new HashMap<String,
String>();
HashMap<String, String> map = null;
//setiap nilai NIM dan NAMA dimasukkan kelam mapMHS
map.put("nama_sektor", jso.getString("nama_sektor"));
daftar_sektor.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, daftar_sektor,
R.layout.list_view,
new String[] { "nama_sektor" }, new int[] {
R.id.txtLstnama_sektor});
setListAdapter(adapter);
}
}