如何将json响应数据发送到自定义ListView? JSON响应是字典吗?

时间:2014-08-25 04:45:58

标签: android json listview

我的活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mediator_id= getIntent().getStringExtra("mediator_id");
    mediator_name= getIntent().getStringExtra("mediator_name");
    new Thread(transaction).start();
    setContentView(R.layout.transaction_log);

    ImageView back=(ImageView) findViewById(R.id.imageView1);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(new Intent(Log.this,Mediator.class));

        }
    });
}

public Runnable transaction = new Runnable() {
    public void run() {

        try {
            if (mediator_id.equals("") || mediator_name.equals("")) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("1", "Improper");
                        // TODO Auto-generated method stub
                    }
                });
            } else {
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();
                HttpPost post = new HttpPost(Configure.APP_BASE_URI_Mediator_TRANSACTIONS);
                post.addHeader("Content-Type", "application/json");
                json.put("no", mediator_id);

                post.setEntity(new StringEntity(json.toString()));

                response = client.execute(post);

                final int status = response.getStatusLine().getStatusCode();
                if (status != HttpStatus.SC_OK) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            Toast.makeText(Log.this,
                                    "Unable to connectggggggggg. Please try again.",
                                    Toast.LENGTH_LONG).show();

                        }
                    });

                } else {
                    InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    String a = convertStreamToString(in);
                    JSONObject jObject = new JSONObject(a);
                    int result = jObject.getInt("status");
                    System.out.print("result+++++++++" + result);
                    Log.d("Result", jObject.getString("message"));
                    final String transObj = jObject.getString("message");

                    if (result == 1) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Intent intent = new Intent(Log.this, Mediator.class);
                                intent.putExtra("mediator_id", mediator_id);
                                intent.putExtra("mediator_name", mediator_name);
                                startActivity(intent);
                                Toast.makeText(Log.this, " Login Successful" + transObj, Toast.LENGTH_LONG).show();
                            }
                        });

                        Intent intent = new Intent(TransactionLog.this, TransactionLog.class);
                        startActivity(intent);
                    } else if (result == 0) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Intent intent = new Intent(TransactionLog.this, Operator_Transactions.class);
                                intent.putExtra("mediator_id", mediator_id);
                                intent.putExtra("mediator_name", mediator_name);
                                startActivity(intent);
                                // TODO Auto-generated method stub
                                Toast.makeText(Log.this, "login details are wrong", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                }
            }
        } catch (final Exception e) {
            //                Toast.makeText( Log.this, "login details are wrong"+e.toString(),Toast.LENGTH_LONG).show();
            Log.d("error", "+++++++" + e.toString());
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Toast.makeText(Log.this,
                            "Unable to connect. Please try again.************** " + e.toString(),
                            Toast.LENGTH_LONG).show();

                }
            });
        }
    }
};

我得到了像这样的JSON响应:

  

d /结果(3928):   {" 19" {" SENDER_ID":" 1210"" M":" 200"&#34 ;介体":" 121030910807"" reciever":" 508505"" ID":" 21" " sender_no":" 508505""备注":" NULL"" sender_no":" 12""类型":"书籍"" DATE_TIME":" 2014年8月22日   19:45:06"}" 22" {" SENDER_ID":" 121030910805"" M":&#34 ; 50.0""介体":" 121030910807"" reciever":" 508505"" ID&#34 ;:" 24"" sender_no":" 508505""备注":" NULL"&#34 ; sender_no":" 121030910805""类型":"书籍"" DATE_TIME":" 2014-08- 23   11:22:17"}" 17":

如何将此数据发送到ListView?

我的自定义ListView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/relMain"
    android:background="@drawable/roundedcorner1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="5dp"
        android:text="DateTime                        :" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Type: " />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text=" Number            : " />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="Amount:" />

    <TextView
        android:id="@+id/transaction_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_marginLeft="39dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="15-08-2014/12:30:00" />

    <TextView
        android:id="@+id/transaction_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/transaction_date"
        android:layout_below="@+id/transaction_date"
        android:text="Withdrawal" />

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_alignLeft="@+id/transaction_type"
        android:text="234561843901" />

    <TextView
        android:id="@+id/amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignLeft="@+id/number"
        android:text="500.00" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="450dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="14dp"
        android:src="@drawable/line1" />

</RelativeLayout>

交易适配器代码:

public class TransactionLog_Adapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;

    ArrayList<Item> data = new ArrayList<Item>();

    public TransactionLog_Adapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;

        Item myImage = data.get(position);
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(parent.getLayoutParams());

        for (int j = 0; j < myImage.getName().length; j++) {

            row = inflater.inflate(layoutResourceId, parent, false);

            RelativeLayout linearMain = (RelativeLayout) row
                    .findViewById(R.id.relMain);
            TextView transaction_date = (TextView) row.findViewById(R.id.transaction_date);
            //transaction_date.setText(myImage.name[j]);

            TextView transaction_type = (TextView) row.findViewById(R.id.transaction_type);
            //transaction_type.setText(myImage.name[j]);

            TextView aadhar_number = (TextView) row.findViewById(R.id.number);
            //aadhar_number.setText(myImage.name[j]);

            TextView amount = (TextView) row.findViewById(R.id.amount);
            //amount.setText(myImage.name[j]);
        }
        return row;
    }

您能否告诉我如何将JSON响应发送到ListView

1 个答案:

答案 0 :(得分:0)

尝试http://jsongen.byingtondesign.com/链接。它将解析JSON以创建客户端源文件以模拟JSON数据结构。然后使用 Gson 来解析更简单的响应

Gson集成教程http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html