java - php 页面中的 JSON 值未显示

标签 java android json

虽然这只是一个粗略的草图..我只是想看看它是否能工作..任何时候我运行该程序它都不会崩溃并且不会显示任何内容。 logcat 上显示的错误是来自 php 页面的 JSON 结果。我不知道为什么 Postexecute() 方法不起作用...

这是错误

09-14 18:21:07.079: E/JSON(21310): {"tag":"getTopic","success":1,"error":0,"data":[{"title":"Facebook finally rolls GRAPH Search","tid":"81","time":"2013-06-22 10:05:18"},{"title":"What is Ubuntu for Android?","tid":"69","time":"2013-06-22 10:18:00"},{"title":"Android and Windows on same device(samsung)","tid":"98","time":"2013-06-22 10:18:35"},{"title":"\"Tweet\" has been verified as a real word in English","tid":"67","time":"2013-06-22 10:27:33"},{"title":"Instagram now has video recording","tid":"97","time":"2013-06-22 10:27:45"},{"title":"An Open Letter to DBanj","tid":"70","time":"2013-06-22 10:31:41"},{"title":"MTN call rate wahala","tid":"84","time":"2013-06-22 10:32:44"},{"title":"Types of friends essential for women","tid":"33","time":"2013-06-22 10:45:41"},{"title":"Not to be loved in return","tid":"106","time":"2013-06-22 18:15:06"},{"title":"I am in love with you","tid":"107","time":"2013-06-22 18:28:56"},{"title":"I hate it!!!","tid":"115","time":"2013-06-23 19:06:26"},{"title":"Spoils of love","tid":"116","time":"2013-06-23 19:14:43"},{"title":"An Apology Letter from Men To Women","tid":"117","time":"2013-06-26 12:43:02"},{"title":"Ramadan Starts tomorrow (in sha Allah) !!!","tid":"120","time":"2013-07-09 15:06:09"},{"title":"Preventing infidelity on your marriage","tid":"29","time":"2013-07-14 12:06:31"},{"title":"How to have a healthy relationship","tid":"121","time":"2013-08-02 17:17:32"},{"title":"ASUU strike not ending anytime soon","tid":"124","time":"2013-08-27 12:56:26"},{"title":"Google Announces Android 4.4 KitKat","tid":"126","time":"2013-09-06 20:51:32"},{"title":"Apple Launches iPhone 5s and iPhone 5c","tid":"127","time":"2013-09-11 15:29:40"}]}

这就是类(class)

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

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

import com.nairation.library.JSONParser;
import com.nairation.library.UserFunctions;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Topic_page extends Activity implements OnItemClickListener{

    public static final String TOPIC_DURATION = "relative";
    public static final String TOPIC_TITLE = "title";
    public static final String TOPIC_PREVIEW = "Testing out";
    public static final String TOPIC_ID = null;
    public static final Object TOPIC_THUMB_URL = null;

    ListView list;
    TopicListAdapter adapter;
    UserFunctions userf;
    JSONParser jpa;
    ArrayList<HashMap<String, String>> topicList = new ArrayList<HashMap<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.topic_page);

        list = (ListView) findViewById(R.id.topic_list);
        adapter = new TopicListAdapter(this, topicList);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);

        try{
            new ServiceSync().execute("mash");
        }catch(Exception e){
            String message = e.getMessage();
        }

    }

    private class ServiceSync extends AsyncTask<String, JSONObject, JSONObject>{

        @Override
        protected JSONObject doInBackground(String... params) {
            // TODO Auto-generated method stub
            String uname = params[0];
            userf = new UserFunctions();
            JSONObject jp = userf.getTopic(uname);
            return jp;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            //super.onPostExecute(result);
            try {
                JSONArray obj = result.getJSONArray("data");

                for (int i = 0; i < result.length(); i++) {
                    JSONObject data = obj.getJSONObject(i);
                    HashMap<String, String> map = new HashMap<String, String>();

                    String title = data.getString(TOPIC_TITLE);
                    String time = data.getString("time");
                    String tid = data.getString("tid");

                    map.put(TOPIC_TITLE, title);
                    map.put("Date", time);

                    topicList.add(map);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Problem with loading the feeds", Toast.LENGTH_LONG).show();
            }
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub

    }
}

那么 JSON 结果没有被处理可能是什么问题..

最佳答案

如果“不工作”意味着您看不到列表中解析的数据,那么您需要在将列表设置为后每当修改基础列表时调用 adapter.notifyDataSetChanged() 方法适配器

关于java - php 页面中的 JSON 值未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804548/

相关文章:

安卓 : LinearLayout with 2 buttons at the bottom

android - Kotlin 适用于 String 未按预期工作

c# - 如何解析属性值之一中带有双引号的 JSON 对象

java - 将十六进制字符串转换为 byte[] 数组时出现错误 "java.lang.StringIndexOutOfBoundsException"

applet 调用 java web 服务

java - Google App Engine 上的 Spring Boot

android - RecyclerView onBindviewholder Dialog clicklistner Animation

java - 如何在fragment Activity 下填充自定义 ListView (扩展Fragment)

ios - 快速调用方法(方法在下一行之前没有完成)

python - bytearray 不是 JSON 可序列化的