JSON解析但无法找出错误:java.lang.String类型的值var无法转换为JSONObject

JSON解析但无法找出错误:java.lang.String类型的值var无法转换为JSONObject,java,android,json,parsing,Java,Android,Json,Parsing,我对android真的很陌生,所以我可能只是做错了。在这个错误上我能找到的唯一一件事就是去掉var,但我正在从中解脱出来 所以我不能完全改变这个。我可能只是做错了,但根据日志,在错误出现并中断程序之前,它会先喝4杯啤酒。这是我的主要课程,任何帮助都将不胜感激 public class MainActivity extends ListActivity { private ProgressDialog pDialog; // URL to get beer private static Str

我对android真的很陌生,所以我可能只是做错了。在这个错误上我能找到的唯一一件事就是去掉var,但我正在从中解脱出来 所以我不能完全改变这个。我可能只是做错了,但根据日志,在错误出现并中断程序之前,它会先喝4杯啤酒。这是我的主要课程,任何帮助都将不胜感激

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get beer
private static String url = "http://www.whatsontap.buildabeer.org/Websites/TapInfo_USAFL00091.js";

// JSON Node names

private static final String TAG_TAPARRAY = "xTapInfo"; //
private static final String TAG_ID = "ItemID"; //ID
private static final String TAG_NAME = "BeerName"; //name
private static final String TAG_BREWERY = "BreweryName"; //email
private static final String TAG_STYLE = "BeerStyle"; //address
private static final String TAG_COLOR = "BeerColor"; //gender
private static final String TAG_PRICE = "Price"; // phone


// contacts JSONArray
JSONArray xTapInfo = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> tapList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String cost = ((TextView) view.findViewById(R.id.brew))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.style))
                    .getText().toString();

            // Starting single contact activity
            Intent in = new Intent(getApplicationContext(),
                    SingleContactActivity.class);
            in.putExtra(TAG_NAME, name);
            in.putExtra(TAG_BREWERY, cost);
            in.putExtra(TAG_STYLE, description);
            startActivity(in);

        }
    });

    // Calling async task to get json
    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                xTapInfo = jsonObj.getJSONArray(TAG_TAPARRAY);

                // looping through All Contacts
                for (int i = 0; i < xTapInfo.length(); i++) {
                    JSONObject c = xTapInfo.getJSONObject(i);
                    System.out.println("HI!");
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String brew = c.getString(TAG_BREWERY);
                    String style = c.getString(TAG_STYLE);
                    String gender = c.getString(TAG_COLOR);





                    // tmp hashmap for single contact
                    HashMap<String, String> xTapInfo = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    xTapInfo.put(TAG_ID, id);
                    xTapInfo.put(TAG_NAME, name);
                    xTapInfo.put(TAG_BREWERY, brew);
                    xTapInfo.put(TAG_STYLE, style);

                    // adding contact to contact list
                    tapList.add(xTapInfo);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, tapList,
                R.layout.list_item, new String[] { TAG_NAME, TAG_BREWERY,
                        TAG_STYLE }, new int[] { R.id.name,
                        R.id.brew, R.id.style });

        setListAdapter(adapter);
    }

}
}

以下是logcat数据:

var xTapInfo = new Array (
{"TOB_ID":"T1", "ItemID":"1  (Nitro)", "BeerName":"Guinness Draught", "BreweryName":"Guinness Ltd", "BeerStyle":"Dry Stout", "BeerColor":"#4A2702", "OtherInfo":"On Nitrogen Tap", "CommercialDescription":"Available in cans, kegs and bottles with nitrogen and carbon dioxide. Pasteurised. Usually called Draught; sometimes called Cold or Extra Cold - same beer, but served colder.    ", "ABV":"4.2%", "LogoGraphic":"Brewery/Guinness.jpg", "LogoWidth":"160", "LogoHeight":"128", "Location":"Ireland", "Price":"$6", "Glassware":"Imperial Pint", "GlasswareGraphic":"Shaker.png", "PourSize":"19.2oz", "RaterNotes":"", "RaterRatings":"", "BAInfo":"79/209/754", "RBInfo":"90/13/1267", "ServingMethod":"Nit", "Keg":"Not tracked", "KegVolume":"0", "ServingsRemaining":"0" },
{"TOB_ID":"T2", "ItemID":"2  (Nitro)", "BeerName":"Left Hand Milk Stout Nitro", "BreweryName":"Left Hand Brewing Co", "BeerStyle":"Sweet Stout", "BeerColor":"#000000", "OtherInfo":"On Nitrogen Tap", "CommercialDescription":"This English style of beer, also known as Sweet Stout or Cream Stout, first appeared in London in the late 1800s. The early brewers touted the health benefits of the milk sugar in this beer which today relates mainly to the increased amount of calories(no real health benefits sorry). The milk sugar adds a well rounded sweetness to this dark beer and makes it an outstanding, year  round stout.    ", "ABV":"6.0%", "LogoGraphic":"Brewery/LeftHand.jpg", "LogoWidth":"228", "LogoHeight":"221", "Location":"CO", "Price":"$6", "Glassware":"Pint", "GlasswareGraphic":"Pint.png", "PourSize":"16oz", "RaterNotes":"", "RaterRatings":"", "BAInfo":"93/418/115076", "RBInfo":"96/78/12028", "ServingMethod":"Nit", "Keg":"Not tracked", "KegVolume":"0", "ServingsRemaining":"0" },
{"TOB_ID":"T3", "ItemID":"3  (Nitro)", "BeerName":"Southern Tier 2X Milk Stout", "BreweryName":"Southern Tier Brewing Co.", "BeerStyle":"Sweet Stout", "BeerColor":"#000000", "OtherInfo":"On Nitrogen Tap", "CommercialDescription":"Double Milk Stout 2 varieties of hops ^ 3 types of malts Milk stout, also called  cream or  sweet stout, is a stout containing lactose, a sugar derived from milk. Because lactose is unfermentable by beer yeast, it adds sweetness and body to the finished beer. Milk stouts have been claimed to be nutritious, and were marketed as such in the early 1900s with claims that would make the FDA wince. One ad read,  Ideal for nursing mothers, for the healthy, for the invalid, and for the worker.  Surely! Of course, we couldnt stop at a traditional milk stout. Ours is a double, an addition to our 2X line, and at 7.5% abv is every bit as delicious as it sounds. To your health!    ", "ABV":"7.5%", "LogoGraphic":"Brewery/SouthernTier.png", "LogoWidth":"128", "LogoHeight":"128", "Location":"NY", "Price":"$4.5", "Glassware":"Pint", "GlasswareGraphic":"Pint.png", "PourSize":"16oz", "RaterNotes":"", "RaterRatings":"", "BAInfo":"86/3818/144141", "RBInfo":"95/3473/161993", "ServingMethod":"Nit", "Keg":"Not tracked", "KegVolume":"0", "ServingsRemaining":"0" },
{"TOB_ID":"T4", "ItemID":"4  (Nitro)", "BeerName":"Boulder Shake Chocolate Porter", "BreweryName":"Boulder Beer", "BeerStyle":"American Porter", "BeerColor":"#3A1702", "OtherInfo":"On Nitrogen Tap", "CommercialDescription":"Our twist on the traditional american porter, Shake chocolate porter is black in color with rich, dark chocolate aromatics and flavors and subtle coffee-like notes. This unique brew blends five different grains, including Chocolate Wheat, that along with cacao nibs create a devilishly delicious chocolate finish with a velvety mouth feel.    ", "ABV":"5.9%", "LogoGraphic":"Brewery/BoulderBeer.jpg", "LogoWidth":"318", "LogoHeight":"159", "Location":"CO", "Price":"$5", "Glassware":"Pint", "GlasswareGraphic":"Pint.png", "PourSize":"16oz", "RaterNotes":"", "RaterRatings":"", "BAInfo":"87/130/101458", "RBInfo":"97/375/232983", "ServingMethod":"Nit", "Keg":"Not tracked", "KegVolume":"0", "ServingsRemaining":"0" },
{"T

04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕       org.json.JSONException: Value var of type java.lang.String cannot be converted to JSONObject
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:160)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at info.androidhive.jsonparsing.MainActivity$GetContacts.doInBackground(MainActivity.java:117)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at info.androidhive.jsonparsing.MainActivity$GetContacts.doInBackground(MainActivity.java:92)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-04 01:49:14.338  21818-21881/info.androidhive.jsonparsing W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
04-04 01:49:14.368  21818-21818/info.androidhive.jsonparsing E/ViewRootImpl﹕ sendUserActionEvent() mView == null
如果有任何帮助,我也将使用此servicehandler文件:

public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

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

    return response;

}

}

一定要共享logcat数据,因为服务器的响应似乎由于某种原因被切断。我正在尝试让它工作,但还没有发现问题所在。添加了logcat数据Kay。除了响应被切断外,该页面似乎没有返回有效的JSON数据。因此我认为这意味着我运气不佳。没有其他方法我可以提取数据吗?这是我试图从中提取的实际站点:,我只是想从本质上提取啤酒列表。