gpt4 book ai didi

java - 我将 Json 存储到一个变量以及如何将 json 值显示到 textview

转载 作者:行者123 更新时间:2023-12-02 03:34:39 25 4
gpt4 key购买 nike

    package com.kovan.thefishchain;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kovan.thefishchain.model.Item;
import com.kovan.thefishchain.model.OrderInfo;

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

import java.io.IOException;

import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;

/**
* Created by Rahul on 6/1/2016.
*/
public class OrderConformation extends Activity implements View.OnClickListener{
private Button placeorder;
private TextView cus_id,order_date,cus_name,cus_email,cust_name,cus_add,cus_mobile,fixedrate,quantity,price,subtotal,tax,discount,grandtotal,subtotallast,
shippingcharge,total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_conformation_page);

cus_id = (TextView) findViewById(R.id.customerr_id);
order_date = (TextView) findViewById(R.id.order_date);
cus_name = (TextView) findViewById(R.id.customer_name);
cus_email = (TextView) findViewById(R.id.customer_email);
cust_name = (TextView) findViewById(R.id.customer_name1);
cus_add = (TextView) findViewById(R.id.customer_address);
cus_mobile = (TextView) findViewById(R.id.customer_number);
fixedrate = (TextView) findViewById(R.id.fixed_rate);
quantity = (TextView) findViewById(R.id.qty);
price = (TextView) findViewById(R.id.price);
subtotal = (TextView) findViewById(R.id.subtotal);
tax = (TextView) findViewById(R.id.tax);
discount = (TextView) findViewById(R.id.discount);
grandtotal = (TextView) findViewById(R.id.rowtotal);
subtotallast = (TextView) findViewById(R.id.subtotal_last);
shippingcharge = (TextView) findViewById(R.id.shipping_charge);
total = (TextView) findViewById(R.id.grand_total);
placeorder = (Button) findViewById(R.id.btn_placeorder);


try
{

Gson gson = new Gson();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OrderConformation.this);
String emailsts = prefs.getString("Orderinfo", "");
String obj = gson.toJson(emailsts);//this will works
Log.i("fine", obj);

JSONObject object = new JSONObject(obj);//here not works,i tried to check in log
String cust_id = object.optString("customer_id");
// Log.i("mmmmmmm",cust_id);
String order_dat = object.getString("ordered_at");
// Log.i("nnnnnnnnn",order_dat);
String name = object.getString("name");
String mobnum = object.getString("phone");
String addr = object.getString("street1");
String rate = object.getString("pricePerUnit");
String quan = object.getString("qty");
String prize = object.getString("pricePerUnit");
String subtot =object.getString("subTotal");
String taxes = object.getString("taxAmount");
String discounts = object.getString("discountAmount");
String total = object.getString("rowTotal");

Toast.makeText(OrderConformation.this,total.toString(),Toast.LENGTH_SHORT).show();

cus_id.setText(cust_id);
order_date.setText(order_dat);
cus_name.setText(name);
cus_mobile.setText(mobnum);
cus_add.setText(addr);
fixedrate.setText(rate);
quantity.setText(quan);
price.setText(prize);
subtotal.setText(subtot);
tax.setText(taxes);
discount.setText(discounts);
grandtotal.setText(total);

} catch (JSONException e1) {
e1.printStackTrace();
}



placeorder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new Orderapi().execute();
Intent intent = new Intent(OrderConformation.this, OrderConformationFinal.class);
startActivity(intent);
Toast.makeText(OrderConformation.this, "hiiiiiiiiiiiiiii", Toast.LENGTH_SHORT).show();

}

});
Gson gson = new Gson();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OrderConformation.this);
String emailsts = prefs.getString("Orderinfo", "");
String obj = gson.toJson(emailsts);
Log.i("fine", obj);

/* OrderInfo odr = new OrderInfo(obj);
Log.i("ord",odr.toString());
Log.i("odrrr", gson.toJson(odr));
String cust_id = odr.getCustomerId("customer_id");
String order_id = odr.getOrderedAt("ordered_id");
String name = odr.getPartner().getName("name");
String mobnum = odr.getPartner().getMobile("phone");
String addr = odr.getPartner(). getStreet1("street1");
cus_id.setText(cust_id);
order_date.setText(order_id);
Log.i("xxx", String.valueOf(cus_id));
Log.i("xxx", String.valueOf(order_date));
*/

}
@Override
public void onClick(View v) {

}
class Orderapi extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
//final String order_idd = getIntent().getStringExtra("orderId");
DefaultHttpClient httpclient = new DefaultHttpClient();
/* HttpGet httGet = new HttpGet("http://52.21.243.46:8080/JStoreConnector/rest/api/store/odoo/order/orderId/placeOrder"
+ order_idd);*/
HttpGet httGet = new HttpGet("http://52.21.243.46:8080/JStore/rest/api/store/odoo/order/order/placeOrder");
HttpResponse resp = null;
try {
resp = httpclient.execute(httGet);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entityResponse = resp.getEntity();
String value = null;
try {
value = EntityUtils.toString(entityResponse);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}

@Override
protected void onPostExecute(String result) {

}

@Override
protected void onPreExecute() {

}
}}

我使用共享首选项概念来存储上一页 json..我检查了将正确的 json...按顺序确认页面尝试部分在这里不起作用..如何将键值设置为 textview...我使用gson在这里..有人可以解决我的问题吗?

最佳答案

你必须使用:

ObjectType object = gson.fromJson(obj);

而不是

JSONObject object = new JSONObject(obj);

其中obj是包含json序列化的字符串

您可能想检查这个example

关于java - 我将 Json 存储到一个变量以及如何将 json 值显示到 textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37591400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com