Back to project page cellar-communicator.
The source code is released under:
GNU General Public License
If you think the Android project cellar-communicator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.vinit.orderplacer; //w ww. jav a2 s. co m import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.AsyncTask; import android.text.InputType; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; public class RetrieveJSON extends AsyncTask<Void, Void, String> { Context context; LinearLayout parent; List<LinearLayout> rowContainers = new ArrayList<LinearLayout>(); List<TextView> productNames = new ArrayList<TextView>(); List<Button> inc = new ArrayList<Button>(); List<EditText> productCount = new ArrayList<EditText>(); List<Button> dec = new ArrayList<Button>(); List<TextView> cost = new ArrayList<TextView>(); Button submit_button, back_button; DumpJSON dumptask; IPGetter get_ip; String category, shared_data, salesman, party, response; HttpClient httpClient; HttpPost httpPost; ProgressDialog dialog; public RetrieveJSON(Context context, LinearLayout parent, String category, String shared_data, String salesman, String party) { this.context = context; this.parent = parent; this.category = category; this.shared_data = shared_data; this.salesman = salesman; this.party = party; dialog = new ProgressDialog(context); } public static LinearLayout.LayoutParams getBasicParams(float weight) { return new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, weight); } @Override protected void onPreExecute() { dialog.setMessage("Loading"); dialog.show(); } @Override protected String doInBackground(Void... arg0) { get_ip = new IPGetter(); httpClient = new DefaultHttpClient(new BasicHttpParams()); httpPost = new HttpPost(get_ip.getIP() + "db.php"); JSONObject sendObj = new JSONObject(); try { sendObj.put("category", category); httpPost.setEntity(new StringEntity(sendObj.toString())); httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Accept-Encoding", "application/json"); httpPost.setHeader("Accept-Language", "en-US"); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); response = EntityUtils.toString(entity); return response; } catch (Exception e) { e.printStackTrace(); return ""; } } @Override protected void onPostExecute(String s) { if (dialog.isShowing()) { dialog.dismiss(); } JSONArray jArr; final JSONArray jOutArr = new JSONArray(); JSONObject jObj; LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.5f); buttonParams.setMargins(40, 20, 40, 10); /* * RETAIN PREVIOUS ARRAY */ try { jArr = new JSONArray(s); if (shared_data != null) { JSONArray jtemp = new JSONArray(shared_data); for (int n = 0; n < jtemp.length(); n++) { jOutArr.put(jtemp.get(n)); } } back_button = new Button(context); back_button.setLayoutParams(buttonParams); back_button.setBackgroundResource(R.drawable.light_selector); back_button.setTextColor(Color.WHITE); back_button.setText("Add more"); /* * ADD MORE OBJECTS */ back_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { JSONObject jOutObj; int i = 0; // TODO: replace with for while (true) { jOutObj = new JSONObject(); try { if (productCount.get(i).getText().toString() .equals("0")) { i++; continue; } jOutObj.put("category", category); jOutObj.put("object", productNames.get(i).getText() .toString()); jOutObj.put("count", productCount.get(i).getText() .toString()); jOutObj.put("price", Integer.toString(Integer.parseInt(cost.get(i).getText().toString()))); jOutArr.put(jOutObj); } catch (JSONException e) { e.printStackTrace(); } catch (IndexOutOfBoundsException e) { break; } i++; } Intent in = new Intent(context, CategoryList.class); in.putExtra("data", jOutArr.toString()); in.putExtra("salesman", salesman); in.putExtra("party", party); context.startActivity(in); } }); /* * I'M DONE */ submit_button = new Button(context); submit_button.setLayoutParams(buttonParams); submit_button.setBackgroundResource(R.drawable.dark_selector); submit_button.setTextColor(Color.WHITE); submit_button.setText("Submit"); submit_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { JSONObject jOutObj; int i = 0; while (true) { jOutObj = new JSONObject(); try { if (productCount.get(i).getText().toString() .equals("0")) { i++; continue; } jOutObj.put("category", category); jOutObj.put("object", productNames.get(i).getText() .toString()); jOutObj.put("count", productCount.get(i).getText() .toString()); jOutObj.put("price", Integer.toString(Integer.parseInt(cost.get(i).getText().toString()))); jOutArr.put(jOutObj); } catch (JSONException e) { e.printStackTrace(); } catch (IndexOutOfBoundsException e) { break; } i++; } Intent in = new Intent(context, SummaryActivity.class); in.putExtra("data", jOutArr.toString()); in.putExtra("salesman", salesman); in.putExtra("party", party); context.startActivity(in); // } }); ScrollView scroll = new ScrollView(context); scroll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); LinearLayout scroll_contain = new LinearLayout(context); scroll_contain.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); scroll_contain.setOrientation(LinearLayout.VERTICAL); for (int i = 0; i < jArr.length(); i++) { jObj = jArr.getJSONObject(i); rowContainers.add(new LinearLayout(context)); rowContainers.get(i).setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); rowContainers.get(i).setOrientation(LinearLayout.HORIZONTAL); productNames.add(new TextView(context)); productNames.get(i).setLayoutParams(getBasicParams(0.6f)); productNames.get(i).setText(jObj.getString("name")); productCount.add(new EditText(context)); productCount.get(i).setLayoutParams(getBasicParams(0.8f)); productCount.get(i).setMinEms(3); productCount.get(i).setInputType(InputType.TYPE_CLASS_NUMBER); productCount.get(i).setGravity(Gravity.CENTER); productCount.get(i).setText("0"); cost.add(new TextView(context)); cost.get(i).setLayoutParams(getBasicParams(0.9f)); cost.get(i).setText(Integer.toString(jObj.getInt("price"))); dec.add(new Button(context)); dec.get(i).setLayoutParams(getBasicParams(0.9f)); dec.get(i).setText("<"); dec.get(i).setTextColor(Color.parseColor("#0aa29c")); dec.get(i).setBackgroundColor(Color.TRANSPARENT); dec.get(i).setOnClickListener( new CounterOnClickListener(productCount.get(i), false)); inc.add(new Button(context)); inc.get(i).setLayoutParams(getBasicParams(0.9f)); inc.get(i).setText(">"); inc.get(i).setTextColor(Color.parseColor("#0aa29c")); inc.get(i).setBackgroundColor(Color.TRANSPARENT); inc.get(i).setOnClickListener( new CounterOnClickListener(productCount.get(i), true)); rowContainers.get(i).addView(productNames.get(i)); rowContainers.get(i).addView(cost.get(i)); rowContainers.get(i).addView(dec.get(i)); rowContainers.get(i).addView(productCount.get(i)); rowContainers.get(i).addView(inc.get(i)); scroll_contain.addView(rowContainers.get(i)); } scroll_contain.addView(back_button); scroll_contain.addView(submit_button); scroll.addView(scroll_contain); parent.addView(scroll); } catch (JSONException e) { e.printStackTrace(); } } }