JSON文件不会在应用程序

时间:2015-12-03 06:23:48

标签: android mysql json

我正在尝试将mysql生成的json文件解析到我的android应用程序中。屏幕根本不显示任何内容。我可以直接在浏览器中提取json文件并输出数据。我不确定为什么我在应用程序中看不到它。如果有人想看一下,我会在这里添加适用的代码。

logcat的

12-02 23:54:10.444 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 60.317ms
12-02 23:54:10.586 2379-2379/com.brasiltradefx.btfxalerts E/c3+U6b5T6IHUG94DJ22teQVKojk=:: PMSeT4Q8FLTjYUOwNpnShvIlw4E=
12-02 23:54:10.642 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 28.796ms
12-02 23:54:10.843 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 17.736ms
12-02 23:54:11.102 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 56.667ms
12-02 23:54:11.294 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 9.199ms
12-02 23:54:11.595 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 78.174ms
12-02 23:54:11.648 2379-2390/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 32.152ms
12-02 23:54:11.720 2379-2390/com.brasiltradefx.btfxalerts I/art: Background sticky concurrent mark sweep GC freed 16851(1095KB) AllocSpace objects, 8(160KB) LOS objects, 58% free, 1549KB/3MB, paused 72.392ms total 254.775ms
12-02 23:54:11.733 2379-2390/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 12.266ms
12-02 23:54:11.837 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 32.684ms
12-02 23:54:12.008 2379-2379/com.brasiltradefx.btfxalerts D/BTFX Alerts: Creating IAB helper.
12-02 23:54:12.013 2379-2379/com.brasiltradefx.btfxalerts D/BTFX Alerts: Starting setup.
12-02 23:54:12.013 2379-2379/com.brasiltradefx.btfxalerts D/IabHelper: Starting in-app billing setup.
12-02 23:54:12.017 2379-2379/com.brasiltradefx.btfxalerts D/BTFX Alerts: Setup finished.
12-02 23:54:12.017 2379-2379/com.brasiltradefx.btfxalerts E/BTFX Alerts: **** BTFX Alerts Error: Problem setting up in-app billing: IabResult: Billing service unavailable on device. (response: 3:Billing Unavailable)
12-02 23:54:12.017 2379-2379/com.brasiltradefx.btfxalerts D/BTFX Alerts: Showing alert dialog: Error: Problem setting up in-app billing: IabResult: Billing service unavailable on device. (response: 3:Billing Unavailable)
12-02 23:54:12.039 2379-2447/com.brasiltradefx.btfxalerts D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-02 23:54:12.306 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 83.896ms
12-02 23:54:12.513 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 16.090ms
12-02 23:54:12.678 2379-2456/com.brasiltradefx.btfxalerts W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8298000 but found 8185470
12-02 23:54:12.793 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 28.924ms
12-02 23:54:12.866 2379-2379/com.brasiltradefx.btfxalerts I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
12-02 23:54:12.984 2379-2447/com.brasiltradefx.btfxalerts I/OpenGLRenderer: Initialized EGL, version 1.4
12-02 23:54:13.322 2379-2447/com.brasiltradefx.btfxalerts W/EGL_emulation: eglSurfaceAttrib not implemented
12-02 23:54:13.322 2379-2447/com.brasiltradefx.btfxalerts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad760120, error=EGL_SUCCESS
12-02 23:54:13.452 2379-2447/com.brasiltradefx.btfxalerts W/EGL_emulation: eglSurfaceAttrib not implemented
12-02 23:54:13.452 2379-2447/com.brasiltradefx.btfxalerts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f9ece0, error=EGL_SUCCESS
12-02 23:54:13.586 2379-2379/com.brasiltradefx.btfxalerts I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
12-02 23:54:14.275 2379-2379/com.brasiltradefx.btfxalerts I/Choreographer: Skipped 40 frames!  The application may be doing too much work on its main thread.
12-02 23:54:20.201 2379-2447/com.brasiltradefx.btfxalerts E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab79e080
12-02 23:54:20.247 2379-2379/com.brasiltradefx.btfxalerts W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
12-02 23:54:22.122 2379-2447/com.brasiltradefx.btfxalerts W/EGL_emulation: eglSurfaceAttrib not implemented
12-02 23:54:22.122 2379-2447/com.brasiltradefx.btfxalerts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa11fec60, error=EGL_SUCCESS
12-02 23:54:22.625 2379-2447/com.brasiltradefx.btfxalerts E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab79e0f0
12-02 23:54:26.348 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 21.880ms
12-02 23:54:26.514 2379-2447/com.brasiltradefx.btfxalerts W/EGL_emulation: eglSurfaceAttrib not implemented
12-02 23:54:26.514 2379-2447/com.brasiltradefx.btfxalerts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0afc040, error=EGL_SUCCESS
12-02 23:54:26.556 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 47.918ms
12-02 23:54:27.095 2379-2447/com.brasiltradefx.btfxalerts E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab79e160
12-02 23:54:27.284 2379-2379/com.brasiltradefx.btfxalerts D/Volley: [1] 1.onResponse: AlertMainActivity
12-02 23:54:27.284 2379-2379/com.brasiltradefx.btfxalerts W/System.err: org.json.JSONException: No value for profilePic
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.brasiltradefx.btfxalerts.AlertMainActivity.parseJsonFeed(AlertMainActivity.java:113)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.brasiltradefx.btfxalerts.AlertMainActivity.access$100(AlertMainActivity.java:31)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.brasiltradefx.btfxalerts.AlertMainActivity$1.onResponse(AlertMainActivity.java:77)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.brasiltradefx.btfxalerts.AlertMainActivity$1.onResponse(AlertMainActivity.java:71)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
12-02 23:54:27.285 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
12-02 23:54:27.286 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-02 23:54:27.286 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at android.os.Looper.loop(Looper.java:148)
12-02 23:54:27.286 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-02 23:54:27.286 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-02 23:54:27.286 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-02 23:54:27.286 2379-2379/com.brasiltradefx.btfxalerts W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-02 23:55:09.625 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 5.831ms
12-03 00:01:53.397 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 27.544ms
12-03 00:13:46.829 2379-2386/com.brasiltradefx.btfxalerts W/art: Suspending all threads took: 22.750ms

MainActivity - 解析json

package com.brasiltradefx.btfxalerts;

import com.brasiltradefx.btfxalerts.adapter.FeedListAdapter;
import com.brasiltradefx.btfxalerts.app.AppController;
import com.brasiltradefx.btfxalerts.data.FeedItem;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

import com.android.volley.Cache;
import com.android.volley.Cache.Entry;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;


/**
 * Created by Deryk on 12/2/2015.
 */
public class AlertMainActivity extends Activity {
    private static final String TAG = AlertMainActivity.class.getSimpleName();
    private ListView listView;
    private FeedListAdapter listAdapter;
    private List<FeedItem> feedItems;
    private String URL_FEED = "http://www.contrariantradefx.info/android/btfxalerts/alert.json";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_main);

        listView = (ListView) findViewById(R.id.list);

        feedItems = new ArrayList<FeedItem>();

        listAdapter = new FeedListAdapter(this, feedItems);
        listView.setAdapter(listAdapter);

        // These two lines not needed,
        // just to get the look of facebook (changing background color & hiding the icon)
        //getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
        //getActionBar().setIcon(
                //new ColorDrawable(getResources().getColor(android.R.color.transparent)));

        // We first check for cached request
        Cache cache = AppController.getInstance().getRequestQueue().getCache();
        Entry entry = cache.get(URL_FEED);
        if (entry != null) {
            // fetch the data from cache
            try {
                String data = new String(entry.data, "UTF-8");
                try {
                    parseJsonFeed(new JSONObject(data));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        } else {
            // making fresh volley request and getting json
            JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
                    URL_FEED, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    VolleyLog.d(TAG, "Response: " + response.toString());
                    if (response != null) {
                        parseJsonFeed(response);
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                }
            });

            // Adding request to volley request queue
            AppController.getInstance().addToRequestQueue(jsonReq);
        }

    }

    /**
     * Parsing json reponse and passing the data to feed view list adapter
     * */
    private void parseJsonFeed(JSONObject response) {
        try {
            JSONArray feedArray = response.getJSONArray("alert");

            for (int i = 0; i < feedArray.length(); i++) {
                JSONObject feedObj = (JSONObject) feedArray.get(i);

                FeedItem item = new FeedItem();
                item.setId(feedObj.getInt("id"));
                item.setName(feedObj.getString("name"));

                // Image might be null sometimes
                String image = feedObj.isNull("image") ? null : feedObj
                        .getString("image");
                item.setImge(image);
                item.setStatus(feedObj.getString("status"));
                item.setProfilePic(feedObj.getString("profilePic"));
                item.setOrder_type(feedObj.getString("order_type"));
                item.setPair(feedObj.getString("pair"));
                item.setRate(feedObj.getString("rate"));
                item.setQuick_target(feedObj.getString("quick_target"));
                item.setLong_target(feedObj.getString("long_target"));
                item.setStop_loss(feedObj.getString("stop_loss"));
                item.setBreak_Even(feedObj.getString("break_even"));
                item.setTimeStamp(feedObj.getString("timeStamp"));


                // url might be null sometimes
                String feedUrl = feedObj.isNull("url") ? null : feedObj
                        .getString("url");
                item.setUrl(feedUrl);

                feedItems.add(item);
            }

            // notify data changes to list adapter
            listAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

FeedItem类 - 这里我注意到第13行的FeedItem是浅灰色的。它有一个错误,说它没有被使用。

public class FeedItem {
    private int id;
    private String name, status, image, profilePic, timeStamp, url, order_type, pair, rate, quick_target, long_target, stop_loss, break_even;

    public FeedItem() {
    }

    public FeedItem(int id, String name, String image, String status,
                    String profilePic, String timeStamp, String url, String order_type, String pair, String rate, String quick_target, String long_target,
    String stop_loss, String break_even) {
        super();
        this.id = id;
        this.name = name;
        this.image = image;
        this.status = status;
        this.profilePic = profilePic;
        this.timeStamp = timeStamp;
        this.url = url;
        this.order_type = order_type;
        this.pair = pair;
        this.rate = rate;
        this.quick_target = quick_target;
        this.long_target = long_target;
        this.stop_loss = stop_loss;
        this.break_even = break_even;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImge() {
        return image;
    }

    public void setImge(String image) {
        this.image = image;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getProfilePic() {
        return profilePic;
    }

    public void setProfilePic(String profilePic) {
        this.profilePic = profilePic;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getOrder_type() {
        return order_type;
    }

    public void setOrder_type(String order_type) {
        this.order_type = order_type;
    }

    public String getPair() {
        return pair;
    }

    public void setPair(String pair) {
        this.pair = pair;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }

    public String getQuick_target() {
        return quick_target;
    }

    public void setQuick_target(String quick_target) {
        this.quick_target = quick_target;
    }

    public String getLong_target() {
        return long_target;
    }

    public void setLong_target(String long_target) {
        this.long_target = long_target;
    }

    public String getStop_loss() {
        return stop_loss;
    }

    public void setStop_loss(String stop_loss) {
        this.stop_loss = stop_loss;
    }

    public String getBreak_Even() {
        return break_even;
    }

    public void setBreak_Even(String break_even) { this.break_even = break_even; }
}

FeedListAdapter

public class FeedListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<FeedItem> feedItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
        this.activity = activity;
        this.feedItems = feedItems;
    }
    @Override
    public int getCount() {
        return feedItems.size();
    }

    @Override
    public Object getItem(int location) {
        return feedItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.feed_item, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        TextView name = (TextView) convertView.findViewById(R.id.name);
        TextView timestamp = (TextView) convertView
                .findViewById(R.id.timestamp);
        TextView statusMsg = (TextView) convertView
                .findViewById(R.id.txtStatusMsg);
        TextView url = (TextView) convertView.findViewById(R.id.txtUrl);

        TextView order_type = (TextView) convertView.findViewById(R.id.txtOrderType);
        TextView pair = (TextView) convertView.findViewById(R.id.txtPair);
        TextView rate = (TextView) convertView.findViewById(R.id.txtRate);
        TextView quick_target = (TextView) convertView.findViewById(R.id.txtQuick_Target);
        TextView long_target = (TextView) convertView.findViewById(R.id.txtLong_Target);
        TextView stop_loss = (TextView) convertView.findViewById(R.id.txtStop_Loss);
        TextView break_even = (TextView) convertView.findViewById(R.id.txtBreak_Even);
        NetworkImageView profilePic = (NetworkImageView) convertView.findViewById(R.id.profilePic);
        FeedImageView feedImageView = (FeedImageView) convertView
                .findViewById(R.id.feedImage1);

        FeedItem item = feedItems.get(position);

        name.setText(item.getName());
        order_type.setText(item.getOrder_type());
        pair.setText(item.getPair());
        rate.setText(item.getRate());
        quick_target.setText(item.getQuick_target());
        long_target.setText(item.getLong_target());
        stop_loss.setText(item.getStop_loss());
        break_even.setText(item.getBreak_Even());

        // Converting timestamp into x ago format
        CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                Long.parseLong(item.getTimeStamp()),
                System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
        timestamp.setText(timeAgo);

        // Chcek for empty status message
        if (!TextUtils.isEmpty(item.getStatus())) {
            statusMsg.setText(item.getStatus());
            statusMsg.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            statusMsg.setVisibility(View.GONE);
        }

        // Checking for null feed url
        if (item.getUrl() != null) {
            url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                    + item.getUrl() + "</a> "));

            // Making url clickable
            url.setMovementMethod(LinkMovementMethod.getInstance());
            url.setVisibility(View.VISIBLE);
        } else {
            // url is null, remove from the view
            url.setVisibility(View.GONE);
        }
        // user profile pic
        profilePic.setImageUrl(item.getProfilePic(), imageLoader);

        // Feed image
        if (item.getImge() != null) {
            feedImageView.setImageUrl(item.getImge(), imageLoader);
            feedImageView.setVisibility(View.VISIBLE);
            feedImageView
                    .setResponseObserver(new FeedImageView.ResponseObserver() {
                        @Override
                        public void onError() {
                        }

                        @Override
                        public void onSuccess() {
                        }
                    });
        } else {
            feedImageView.setVisibility(View.GONE);
        }

        return convertView;
    }

}

0 个答案:

没有答案