Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Although I know that this question has already been posted on stack overflow many times, but I have tried all solutions and nothing worked for me.

I am trying to display data in List View. Data is stored in the json format and while fetching data it seems like android is not reading the JSON Array and hence data is not coming up in List View and it remains empty.

Java file:

public class ReadResult extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> ResultFetch;
private static String url_readResult = "http://10.0.2.2/Result-Viewer/php/ReadData.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_SCORE = "Score";
private static final String TAG_SEMESTER = "Semester";
private static final String TAG_PRODUCTS = "products";
JSONArray products = null;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.read_result);
    list = (ListView) getListView();
    ResultFetch = new ArrayList<HashMap<String, String>>();
    new LoadResult().execute();
}

class LoadResult extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ReadResult.this);
        pDialog.setMessage("Loading Result. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        List<NameValuePair> param = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_readResult, "GET",
                param);


        Log.d("Result: ", json.toString());
        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                JSONObject object = new JSONObject(json.toString());
                products = object.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);
                    String Semester = c.getString(TAG_SEMESTER);
                    String Score = c.getString(TAG_SCORE);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_SEMESTER, Semester);
                    map.put(TAG_SCORE, Score);
                    ResultFetch.add(map);
                }
            } else {
                 Toast toast = Toast.makeText(getApplicationContext(),
                 "No Result Found", Toast.LENGTH_SHORT);
                 toast.show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {

                ListAdapter adapter = new SimpleAdapter(ReadResult.this,
                        ResultFetch, R.layout.list_item, new String[] {
                                TAG_SEMESTER, TAG_SCORE }, new int[] {
                                R.id.semester, R.id.score });

                list.setAdapter(adapter);

            }
        });

    }

}

}

php file :

<?php
session_start();
$cid = $_SESSION["cid"];
$rno = $_SESSION["rno"];
$response = array();

$conn=new PDO('mysql:host=localhost;dbname=result','root' ,'');
$result=$conn->query("Select * from $cid where RegistrationNumber =     '$rno'");

if($result->rowcount()>0)
{
$response["products"] = array();
foreach($result as $row)
{
$product["Semester"] = $row["Semester"];
$product["Score"] = $row["Score"];
$response["success"] = 1;
array_push($response["products"], $product);
echo json_encode($response);
}
}
else
{
$response["success"] = 0;
$response["message"] = "No record found.";
echo json_encode($response);
}

?>

I have passed the cid,rno and password through sign in page and the following is the json output generated by running read data page.

json output :

{"products":[{"Semester":"0","Score":"0"}],"success":1}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
189 views
Welcome To Ask or Share your Answers For Others

1 Answer

I got a comment on your code that might help. The following line is useless

JSONObject object = new JSONObject(json.toString());

as you can just use json as your JSONObject instead of creating a new one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...