JSON解析填充listview Android Studio

时间:2015-10-18 00:51:01

标签: json parsing

我把头发拉过来。经过无数的教程,我认为我发现了一个完美的教程(确切地说是第7个。但是在完成教程之后,我发现JSONparse已被弃用。有人可以给我一个解决方案。我只想读取一个数组来自url并填充listview。

数组是:

{ "lotInfo":[{"lot":"A","spaces":"198","rates":"3.25"},  
{"lot":"B","spaces":"165","rates":"7.50"}]}

MainActivity.Java:

package com.example.sahan.wtf;


import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends ListActivity {

private Context context;
private static String url = "http://192.168.0.199/get_info.php";

private static final String lot = "lot";
private static final String spaces = "spaces";
private static final String rates = "rates";

ArrayList<HashMap<String, String>> jsonlist = new   ArrayList<HashMap<String, String>>();

ListView lv ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new ProgressTask(MainActivity.this).execute();
}


private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;

    public ProgressTask(ListActivity activity) {

        Log.i("1", "Called");
        context = activity;
        dialog = new ProgressDialog(context);
    }

    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_activity, new String[] { lot, spaces, rates }, new int[] { R.id.lot, R.id.spaces, R.id.rates });
        setListAdapter(adapter);
        lv = getListView();

    }

    protected Boolean doInBackground(final String... args) {

        JSONParse jParser = new JSONParser();
        JSONArray json = jParser.getJSONFromUrl(url);

        for (int i = 0; i < json.length(); i++) {

            try {
                JSONObject c = json.getJSONObject(i);
                String vlot = c.getString(lot);

                String vspaces = c.getString(spaces);
                String vrates = c.getString(rates);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(lot, vlot);
                map.put(spaces, vspaces);
                map.put(rates, vrates);


                jsonlist.add(map);
            } catch (JSONException e)
            {
                e.printStackTrace();
            }
        }
        return null;

    }

}


}

JSONParser.Java:

package com.example.sahan.wtf;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class JSONParser {

static InputStream is = null;
static JSONArray jarray = null;
static String json = "";

public JSONParser() {
}

public JSONArray getJSONFromUrl(String url) {

    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("Error....", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        jarray = new JSONArray( builder.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return array;

}
}

我得到的错误是: 错误:(74,13)错误:找不到符号类JSONParse

1 个答案:

答案 0 :(得分:0)

似乎你有一个拼写错误,而不是:

JSONParse jParser = new JSONParser();

应该是:

JSONParser jParser = new JSONParser();