简体   繁体   中英

Data is not fetched through JSON parsing

Data is not fetched through json parsing. I want to fetch data from url and just set it to a textview. please help

private static final String URL_PRODUCTS = "http://ebeautyapp.com/experts/getContactUs.php";


//method for json parcing

private void loadData() {


 StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
    new Response.Listener < String > () {
     @Override
     public void onResponse(String response) {
        try {

         JSONObject jObj = new JSONObject(response);
         String result = jObj.getString("result");


         if (result.equals("success")) {

            JSONArray jsonArray = jObj.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {

             JSONObject jsonObject = jsonArray.getJSONObject(i);
             String contact_id = jsonObject.getString("contact_id");
             String fullname = jsonObject.getString("fullname");
             String moble1 = jsonObject.getString("moble1");
             String mobileno2 = jsonObject.getString("mobileno2");
             String profile_pic = jsonObject.getString("profile_pic");
             String address = jsonObject.getString("address");
             String about_us = jsonObject.getString("about_us");


             fulllnames.setText(fullname);
             mobilenos.setText(moble1);


            }
         } else {

            String status = jObj.getString("status");

            Toast.makeText(getApplicationContext(), "" + status, Toast.LENGTH_SHORT).show();

         }

        } catch (JSONException e) {
         // JSON error
         e.printStackTrace();
         Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
     }
    },
    new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {

     }
    });



 //adding our stringrequest to queue
 Volley.newRequestQueue(this).add(stringRequest);

}

Using this I have got the response from the server. Hope this will solve your problem.

class RetrieveFeedTask extends AsyncTask<Void, Void, String> {

        protected void onPreExecute() {
           // responseView.setText("");
        }

        protected String doInBackground(Void... urls) {
            String API_URL = "http://ebeautyapp.com/experts/getContactUs.php";
            // Do some validation here

            try {
                URL url = new URL(API_URL);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                    bufferedReader.close();
                    return stringBuilder.toString();
                }
                finally{
                    urlConnection.disconnect();
                }
            }
            catch(Exception e) {
                Log.e("ERROR", e.getMessage(), e);
                return null;
            }
        }

        protected void onPostExecute(String response) {
            if(response == null) {
                response = "THERE WAS AN ERROR";
            }
            //            progressBar.setVisibility(View.GONE);
            Log.i("INFO", response);
           // responseView.setText(response);
           // parseJsonData(response);
        }

JSON parsing

private void jsonParsing(String response){
try {

         JSONObject jObj = new JSONObject(response);
         String result = jObj.getString("result");


         if (result.equals("success")) {

            JSONArray jsonArray = jObj.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {

             JSONObject jsonObject = jsonArray.getJSONObject(i);
             String contact_id = jsonObject.getString("contact_id");
             String fullname = jsonObject.getString("fullname");
             String moble1 = jsonObject.getString("moble1");
             String mobileno2 = jsonObject.getString("mobileno2");
             String profile_pic = jsonObject.getString("profile_pic");
             String address = jsonObject.getString("address");
             String about_us = jsonObject.getString("about_us");


             fulllnames.setText(fullname);
             mobilenos.setText(moble1);


            }
         } else {

            String status = jObj.getString("status");

            Toast.makeText(getApplicationContext(), "" + status, Toast.LENGTH_SHORT).show();

         }

        } catch (JSONException e) {
         // JSON error
         e.printStackTrace();
         Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
     } 
}

And use this task as simple by using this

 new RetrieveFeedTask().execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM