1

I have a URL which I have to parse and then reflect its data to its relevant fields. When I am putting my URL in statically format, I am able to get Data and parsing is done. My statically URL is like this

String URL = "http://54.152.108.131/iphone111/getParentInput?child_id=6272&date=1/19/2016";

But Now I want to change its ChldId and date so I made my URL in this format..

            String loginUrl = "http://54.152.108.131/iphone111/getParentInput?child_id="+id+"&date="+date.toString();

My Code is like this

class ParentInputData extends
        AsyncTask<String, String, JSONObject> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected JSONObject doInBackground(String... params) {
        String loginUrl = "http://54.152.108.131/iphone111/getParentInput?child_id="+id+"&date="+date.toString();
     Toast.makeText(getApplicationContext(), "value of loginUrl : " + loginUrl, Toast.LENGTH_LONG).show();
    System.out.println(loginUrl);   

     JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(loginUrl);
        return json;

    }

    @Override
    protected void onPostExecute(JSONObject json) {
        try {

            json_array = json.getJSONArray("CenterInfoData");
            for (int i = 0; i < json_array.length(); i++) {
                JSONObject c = json_array.getJSONObject(i);
                c = c.getJSONObject("ParentInput");

                String wake_up_time = c.getString("wake_up_time");
                String sleep_quality = c.getString("sleep_quality");
                String person_picking_child = c.getString("person_picking_child");
                String pick_up_time = c.getString("pick_up_time");
                String last_diaper_bm = c.getString("last_diaper_bm");
                String last_bottle_feeding = c.getString("last_bottle_feeding");
    }
}

But in this case I am getting Null Pointer Exception.. I debugged My URL but it still showing complete URL having ChildId and date.. Where I am doing Mistakes.. I already tried URLEncoder.. My LogCat is here

01-19 08:09:43.628: E/AndroidRuntime(18018): FATAL EXCEPTION: AsyncTask #3
01-19 08:09:43.628: E/AndroidRuntime(18018): Process: com.MyKidzDay, PID: 18018
01-19 08:09:43.628: E/AndroidRuntime(18018): java.lang.RuntimeException: An error occured while executing doInBackground()
01-19 08:09:43.628: E/AndroidRuntime(18018):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.lang.Thread.run(Thread.java:841)
01-19 08:09:43.628: E/AndroidRuntime(18018): Caused by: java.lang.NullPointerException
01-19 08:09:43.628: E/AndroidRuntime(18018):    at com.KidsTabs.KidSummary$ParentInputData.doInBackground(KidSummary.java:1651)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at com.KidsTabs.KidSummary$ParentInputData.doInBackground(KidSummary.java:1)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-19 08:09:43.628: E/AndroidRuntime(18018):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-19 08:09:43.628: E/AndroidRuntime(18018):    ... 4 more
01-19 08:09:43.644: W/EGL_genymotion(18018): eglSurfaceAttrib not implemented
01-19 08:09:44.084: I/Inside convertstream method(18018): hello

My JSONParser Code is here..

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public String makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if (method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

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

        // return JSON String
        return json;

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
kitkat
  • 142
  • 10

1 Answers1

1

You are getting error because you are displaying Toast in Background thread.

Your doInBackground should looks like below:

@Override
    protected Void doInBackground(Void... params) {
   // Other stuff
   runOnUiThread(new Runnable() {
      @Override
      public void run() {
          Toast.makeText(getApplicationContext(), "value of loginUrl : " + loginUrl, Toast.LENGTH_LONG).show();
            }
        });
      return null;
 }

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • 1
    Sir, If I am removing Toast then still its showing error and I am displaying Toast in onPreExecute() – kitkat Jan 20 '16 at 06:22
  • @kitkat, make sure you are getting proper response from server – Hiren Patel Jan 20 '16 at 06:24
  • Yes sir, When I am debugging my URL then I am getting every Data of child_id and date – kitkat Jan 20 '16 at 06:25
  • I commentd my Toast then even i am getting error of NUllPointerException – kitkat Jan 20 '16 at 06:25
  • @kitkat, please print log of your json – Hiren Patel Jan 20 '16 at 06:35
  • @kitkat, just double click on first line package logcat, let me know at which line you are getting error – Hiren Patel Jan 20 '16 at 06:42
  • I am getting error on my URL... String loginUrl = "http://54.152.108.131/iphone111/getParentInput?child_id="+id+"&date="+date.toString(); – kitkat Jan 20 '16 at 06:46
  • @kitkat, please provide valid url so i can check with response – Hiren Patel Jan 20 '16 at 06:47
  • My valid URL is here Sir, http://54.152.108.131/iphone111/getParentInput?child_id=6272&date=1/19/2016 but its child_id and date should be come from dynamically.. same Child_ID and Data I am getting in my class and I am appending it on my My URL – kitkat Jan 20 '16 at 06:50
  • Hi @Hiren Sir, thnxx you and everyone. I solved it.. actually problem was at date.toString();.... Now I am getting my data. Thank you everyone fro kind support – kitkat Jan 20 '16 at 07:10