Advertisement
Guest User

java

a guest
Feb 3rd, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. package com.androidhive.googleplacesandmaps;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Method;
  5. import java.util.ArrayList;
  6. import org.apache.http.NameValuePair;
  7. import org.apache.http.message.BasicNameValuePair;
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. import android.app.Activity;
  13. import android.content.Intent;
  14. import android.os.AsyncTask;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.widget.EditText;
  18. import android.widget.TextView;
  19. public class RecoProd extends Activity {
  20.     EditText pd;
  21.     TextView error;
  22.  
  23.     ArrayList<NameValuePair> postParameters;
  24.     /** Called when the activity is first created. */
  25.     @Override
  26.     public void onCreate(Bundle savedInstanceState) {
  27.        
  28.         try {
  29.             Class<?> strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread()
  30.                     .getContextClassLoader());
  31.  
  32.             Class<?> threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread
  33.                     .currentThread().getContextClassLoader());
  34.  
  35.             Class<?> threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true,
  36.                     Thread.currentThread().getContextClassLoader());
  37.  
  38.             Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass);
  39.  
  40.             Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll");
  41.             Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog");
  42.             Method buildMethod = threadPolicyBuilderClass.getMethod("build");
  43.  
  44.             Constructor<?> threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor();
  45.             Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance();
  46.  
  47.             Object obj = detectAllMethod.invoke(threadPolicyBuilderObject);
  48.  
  49.             obj = penaltyMethod.invoke(obj);
  50.             Object threadPolicyObject = buildMethod.invoke(obj);
  51.             setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject);
  52.  
  53.         } catch (Exception ex) {
  54.             String TAG = null;
  55.             Log.w(TAG, ex);
  56.         }
  57.         super.onCreate(savedInstanceState);
  58.         setContentView(R.layout.recomain);
  59.         Intent myIntent = getIntent(); // getting the value from the previous activity
  60.         String lbl_name= myIntent.getStringExtra("lbl_name");//< get lbl_name from Intent
  61.         pd = (EditText) findViewById(R.id.name);
  62.  
  63.         error = (TextView) findViewById(R.id.recomain);
  64.  
  65.        
  66.                 postParameters = new ArrayList<NameValuePair>();
  67.                 postParameters.add(new BasicNameValuePair("product", lbl_name
  68.                         ));
  69.  
  70.  
  71.                 // String valid = "1";
  72.  
  73.                 DownloadWebPageTask dw = new DownloadWebPageTask();
  74.                 dw.execute("");
  75.  
  76.     }
  77.  
  78.     private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
  79.         @Override
  80.         protected String doInBackground(String... urls) {
  81.             String response = null;
  82.             for (String url : urls) {
  83.                 try {
  84. //                    response = CustomHttpClient.executeHttpPost(url, postParameters);  
  85.                     response = CustomHttpClient.executeHttpPost("https://192.168.1.7/abc/check2.php", postParameters);  
  86.                     String res = response.toString();
  87.                     // res = res.trim();
  88.                     res = res.replaceAll("\\s+", "");
  89.                     // error.setText(res);
  90.                     try{
  91.                         res = "";
  92.                   JSONArray jArray = new JSONArray(res);
  93.                         for(int i=0;i<jArray.length();i++){
  94.                                 JSONObject json_data = jArray.getJSONObject(i);
  95.                                 Log.i("prod_id","id: "+json_data.getInt("prod_id")+
  96.                                         ", prod_name: "+json_data.getString("prod_name")+
  97.                                         ", prod_category: "+json_data.getString("prod_category")+
  98.                                         ", prod_cost: "+json_data.getDouble("prod_cost")
  99.                                 );
  100.                                 //Get an output to the screen
  101.                                 res += "\n" + json_data.getInt("prod_id") + " -> "+ json_data.getString("prod_name");
  102.                         }
  103.                   response = res;
  104.                 }
  105.                 catch(JSONException e){
  106.                         Log.e("log_tag", "Error parsing data "+e.toString());
  107.                 }
  108.            
  109.                 try{
  110.                  error.setText(res);
  111.                 }
  112.                 catch(Exception e){
  113.                  Log.e("log_tag","Error in Display!" + e.toString());;          
  114.                 }  
  115.            }
  116.                  catch (Exception e) {
  117.             Log.e("log_tag","Error in http connection!!" + e.toString());    
  118.            }
  119.             }
  120.             return response;
  121.                          
  122.                }
  123.         @Override
  124.         protected void onPostExecute(String result) {
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement