Issue
I have a problem with retrieving JSON. My JSON looks like this:
[{"Kli_nazwa":"miechy ","Kli_adres":"miechowity ","Kli_nip":"12"},{"Kli_nazwa":"mi? ","Kli_adres":"szp 39","Kli_nip":"19"}]
The app is throwing a NullPointerException in onPostExecute, even if thr JSON was retrieved correctly. What it's going on? I am totally stuck.
private class PHPConnection_Klient extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
wynik_klienci = getResponseKli();
return wynik_klienci;
}
@Override
protected void onPostExecute(String wynik_klienci) {
// TODO Auto-generated method stub
super.onPostExecute(wynik_klienci);
System.out.println(wynik_klienci);
Gson g1 = new Gson();
List<Klient> klient = Arrays.asList(g1.fromJson(wynik_klienci,
Klient[].class));
try {
klientDao.delete(klientDao.queryForAll());
if (klient != null) {
for (Klient k : klient) {
klientDao.create(k);
}
}
} catch (final SQLException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
e.printStackTrace();
}
});
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
private String getResponseKli() {
HttpClient client = new DefaultHttpClient();
HttpGet request1 = new HttpGet(
"myURL");
try {
HttpResponse response1 = client.execute(request1);
String w1 = EntityUtils.toString(response1.getEntity());
return w1;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
Errors:
03-19 10:29:06.395: W/dalvikvm(5851): threadid=1: thread exiting with uncaught exception (group=0x40be01f8)
03-19 10:29:06.403: E/AndroidRuntime(5851): FATAL EXCEPTION: main
03-19 10:29:06.403: E/AndroidRuntime(5851): java.lang.NullPointerException
03-19 10:29:06.403: E/AndroidRuntime(5851): at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
03-19 10:29:06.403: E/AndroidRuntime(5851): at com.example.inzynierka.MainActivity$PHPConnection_Klient.onPostExecute(MainActivity.java:627)
03-19 10:29:06.403: E/AndroidRuntime(5851): at com.example.inzynierka.MainActivity$PHPConnection_Klient.onPostExecute(MainActivity.java:1)
03-19 10:29:06.403: E/AndroidRuntime(5851): at android.os.AsyncTask.finish(AsyncTask.java:602)
03-19 10:29:06.403: E/AndroidRuntime(5851): at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-19 10:29:06.403: E/AndroidRuntime(5851): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-19 10:29:06.403: E/AndroidRuntime(5851): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 10:29:06.403: E/AndroidRuntime(5851): at android.os.Looper.loop(Looper.java:137)
03-19 10:29:06.403: E/AndroidRuntime(5851): at android.app.ActivityThread.main(ActivityThread.java:4514)
03-19 10:29:06.403: E/AndroidRuntime(5851): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 10:29:06.403: E/AndroidRuntime(5851): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 10:29:06.403: E/AndroidRuntime(5851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-19 10:29:06.403: E/AndroidRuntime(5851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-19 10:29:06.403: E/AndroidRuntime(5851): at dalvik.system.NativeStart.main(Native Method)
And this is how i'm executing two PHPConnections:
PHPConnection_Klient php_kli = new PHPConnection_Klient();
php_kli.execute();
PHPConnection_Kategoria php_kt = new PHPConnection_Kategoria();
php_kt.execute();
Solution
you are returining null and you should return wynik_klienci
@Override
protected String doInBackground(String... params) {
wynik_klienci = getResponseKli();
return wynik_klienci;
}
since you are hiding the scope of the class member wynik_klienci
, calling the formal parameter of onPostExecute
the same way of it
Answered By - Blackbelt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.