Java 我将Json存储到一个变量,以及如何向textview显示Json值 package com.kovan.thefishchain; 导入android.app.Activity; 导入android.content.Intent; 导入android.c
我将Json存储到一个变量,以及如何向textview显示Json值Java 我将Json存储到一个变量,以及如何向textview显示Json值 package com.kovan.thefishchain; 导入android.app.Activity; 导入android.content.Intent; 导入android.c,java,android,json,Java,Android,Json,我将Json存储到一个变量,以及如何向textview显示Json值 package com.kovan.thefishchain; 导入android.app.Activity; 导入android.content.Intent; 导入android.content.SharedReferences; 导入android.os.AsyncTask; 导入android.os.Bundle; 导入android.preference.PreferenceManager; 导入android.ut
package com.kovan.thefishchain;
导入android.app.Activity;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.preference.PreferenceManager;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.gson.gson;
导入com.kovan.thefishchain.model.Item;
导入com.kovan.thefishchain.model.OrderInfo;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
导入cz.msebera.android.httpclient.HttpEntity;
导入cz.msebera.android.httpclient.HttpResponse;
导入cz.msebera.android.httpclient.client.methods.HttpGet;
导入cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
导入cz.msebera.android.httpclient.util.EntityUtils;
/**
*由Rahul于2016年6月1日创建。
*/
公共类OrderConformation扩展活动实现View.OnClickListener{
私人按钮订单;
私有文本查看客户id、订单日期、客户名称、客户电子邮件、客户名称、客户添加、客户移动、固定日期、数量、价格、小计、税费、折扣、总计、小计、,
运费,合计;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、订单确认页面);
cus_id=(TextView)findViewById(R.id.customerr_id);
订单日期=(TextView)findViewById(R.id.order\u date);
cus_name=(TextView)findViewById(R.id.customer_name);
客户电子邮件=(TextView)findViewById(R.id.customer\u电子邮件);
客户名称=(TextView)findViewById(R.id.customer\u name 1);
cus_add=(TextView)findViewById(R.id.customer_地址);
cus_mobile=(TextView)findViewById(R.id.customer_number);
fixedrate=(TextView)findViewById(R.id.固定利率);
数量=(TextView)findViewById(R.id.qty);
price=(TextView)findViewById(R.id.price);
小计=(TextView)findViewById(R.id.小计);
tax=(TextView)findViewById(R.id.tax);
折扣=(TextView)findViewById(R.id.discount);
grandtotal=(TextView)findViewById(R.id.rowtotal);
subtotallast=(TextView)findViewById(R.id.subtotal\u last);
shippingcharge=(TextView)findViewById(R.id.运费);
总计=(TextView)findViewById(R.id.grand_总计);
placeorder=(按钮)findViewById(R.id.btn\u placeorder);
尝试
{
Gson Gson=新的Gson();
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(OrderConformation.this);
String emailsts=prefs.getString(“Orderinfo”和“”);
String obj=gson.toJson(emailsts);//这将起作用
Log.i(“罚款”,obj);
JSONObject object=newjsonobject(obj);//这里不起作用,我试图签入日志
字符串cust_id=object.optString(“customer_id”);
//Log.i(“mmmmmmm”,客户id);
String order_dat=object.getString(“ordered_at”);
//日志i(“nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn;
字符串名称=object.getString(“名称”);
String mobnum=object.getString(“电话”);
stringaddr=object.getString(“street1”);
字符串速率=object.getString(“pricePerUnit”);
String quan=object.getString(“数量”);
String prize=object.getString(“pricePerUnit”);
String subtot=object.getString(“subTotal”);
字符串taxes=object.getString(“taxAmount”);
字符串折扣=object.getString(“折扣量”);
字符串总数=object.getString(“rowTotal”);
Toast.makeText(OrderConformation.this,total.toString(),Toast.LENGTH_SHORT.show();
cus_id.setText(cust_id);
订单日期设置文本(订单日期);
cus_name.setText(名称);
cus_mobile.setText(mobnum);
cus_add.setText(addr);
fixedrate.setText(速率);
数量.setText(quan);
price.setText(prize);
小计.设置文本(小计);
tax.setText(taxes);
折扣.setText(折扣);
grandtotal.setText(总计);
}捕获(JSONException e1){
e1.printStackTrace();
}
placeorder.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
新建Orderapi().execute();
意向意向=新意向(OrderConformation.this、OrderConformationFinal.class);
星触觉(意向);
Toast.makeText(OrderConformation.this,“hiiiii”,Toast.LENGTH\u SHORT.show();
}
});
Gson Gson=新的Gson();
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(OrderConformation.this);
String emailsts=prefs.getString(“Orderinfo”和“”);
字符串obj=gson.toJson(emailsts);
Log.i(“罚款”,obj);
/*OrderInfo odr=新的OrderInfo(obj);
Log.i(“ord”,odr.toString());
Log.i(“odrrr”,gson.toJson(odr));
字符串cust_id=odr.getCustomerId(“customer_id”);
字符串order_id=odr.getOrderedAt(“ordered_id”);
字符串名称=odr.getPartner().getName(“名称”);
字符串mobnum=odr.getPartner().getMobile(“电话”);
字符串addr=odr.getPartner().getStreet1(“street1”);
cus_id.setText(cust_id);
order_date.setText(订单id);
Log.i(“xxx”,String.valueOf(cus_id));
Log.i(“xxx”,String.valueOf(订单日期));
*/
}
@凌驾
公共void onClick(视图v){
}
类Orderapi扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//最终字符串顺序_id=getIntent().getStringExtra(“orderId”);
DefaultHttpClien
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() {
}
}}
ObjectType object = gson.fromJson(obj);
JSONObject object = new JSONObject(obj);
private static final String PREFERENCE_NAME = "App Name";
private static final String OBJCETNAME = "Object Name";
/**
* Insert string value in Shared Preferences
*
* @param context of application
* @param value to store in preferences
* @param key using which value is mapped
* @return
*/
public static boolean putStringInPreferences(final Context context,
final String value, final String key) {
final SharedPreferences sharedPreferences = context
.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
return true;
}
/**
* Get Data from preferance
*
* @param context
* @param defaultValue
* @param key
* @return
*/
public static String getStringFromPreferences(final Context context,
final String defaultValue, final String key) {
final SharedPreferences sharedPreferences = context
.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
final String temp = sharedPreferences.getString(key, defaultValue);
return temp;
}
/**
*
*
* @param context
* @param users
*/
public static void setObjectInPreferance(Context context, String users) {
PreferanceUtils.putStringInPreferences(context, users,
OBJCETNAME);
}
/**
*
*
* @param context
* @return
*
*/
public static Object getObjectFromPreferance(Context context) {
return new Gson().fromJson(PreferanceUtils.getStringFromPreferences(context, "",
OBJCETNAME), Object.class);
}