Hi I'm new in the world of android. So I tested a tutorial do fetch data from a XAMPP database and list it in my app with listview. Everything is like in the tutorial, so why my app doesnt't work? When I start the app i got an error message and the app crushes.
Thank you for your help!
public class MainActivity extends ActionBarActivity {
private TextView responseTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.responseTextView = (TextView) this.findViewById(R.id.responseTextView);
new GetAllCustomerTask().execute(new ApiConnector());
}
public void setTextToTextView(JSONArray jsonArray)
{
String s = "";
for(int i=0; i<jsonArray.length();i++){
JSONObject json = null;
try {
json = jsonArray.getJSONObject(i);
s = s +
"ID : "+json.getInt("id")+"\n"+
"Name : "+json.getString("name")+"\n\n";
} catch (JSONException e) {
e.printStackTrace();
}
}
this.responseTextView.setText(s);
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
return params[0].GetAllCustomers();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
setTextToTextView(jsonArray);
}
}
}
public class ApiConnector {
public JSONArray GetAllCustomers()
{
// URL for getting all customers
String url = "http://localhost/getAllCustomers.php";
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
}
This is my logcat
04-06 22:17:21.550 23914-23914/diplomarbeit.at.dbfetchcontenttester E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: diplomarbeit.at.dbfetchcontenttester, PID: 23914
java.lang.NullPointerException
at diplomarbeit.at.dbfetchcontenttester.MainActivity.setTextToTextView(MainActivity.java:34)
at diplomarbeit.at.dbfetchcontenttester.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:66)
at diplomarbeit.at.dbfetchcontenttester.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:53)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)