Lucky recipe for android applets: android applets
Lucky recipe for android applets
I have just finished a five-day open course on android, And I have gained a lot. Write it down and record it! (Android is occasionally used because of open classes of school and enterprise classes, so I have only learned this for a few days, so I don't like it)
At first, I learned that this open class had to build a project, but it was still awesome. I don't know what it is. Later, I looked at the free APIs of aggregated data and thought about what I had to eat today during the winter vacation. Finally, I had the idea of making this lucky recipe.
What is the length of this app? The following figure shows it:
The first one is the main interface, enter a number you like (1-80000), and then the corresponding recipe will be returned in the second interface. You are not mistaken (80 thousand dishes, I was shocked when I started using this api !)
Next, let's put the code of the interface and process the java code of the corresponding interface:
1 public class Juhedemo {// call the interface code 2 public static final String DEF_CHATSET = "UTF-8"; 3 public static final int DEF_CONN_TIMEOUT = 30000; 4 public static final int DEF_READ_TIMEOUT = 30000; 5 public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 "; 6 7 // configure the KEY 8 public static final String APPKEY = "This will not tell you, apply for your own account 9 10 // 4. view Details by recipe ID 11 public menubean getRequest4 (String num) {12 menubean mu = new menubean (); 13 String result = null; 14 String title, ingredient, burden; 15 ArrayList <String> B = new ArrayList <> (); 16 String url = "http://apis.juhe.cn/cook/queryid"; // request interface address 17 Map params = new HashMap (); // request parameter 18 params. put ("id", num); // recipe ID 19 params. put ("key", APPKEY); // application APPKEY (application details page query) 20 params. put ("dtype "," Json "); // The format of the returned data, xml or json. The default value is json 21 Log. e ("MyLog", "= num =" + num); 22 try {23 result = net (url, params, "GET "); 24 Log. e ("MyLog", "=== result =" + result); 25 org. json. JSONObject object = new org. json. JSONObject (result); 26 if (object. getInt ("error_code") = 0) {27 org. json. JSONObject resultObject = object. getJSONObject ("result"); 28 org. json. JSONArray array = resultObject. getJSONArray ("data"); 29 Mu. setTitle (array. getJSONObject (0 ). getString ("title"); 30 mu. setIngredient (array. getJSONObject (0 ). getString ("ingredients"); 31 mu. setBurden (array. getJSONObject (0 ). getString ("burden"); 32 org. json. JSONObject o1 = array. getJSONObject (0); 33 org. json. JSONArray a1 = o1.getJSONArray ("steps"); 34 for (int I = 0; I <a1.length (); I ++) {35 String step = a1.getJSONObject (I ). getString ("step"); 36 B. add (step); 37} 3 8 mu. setB (B); 39} 40 // JSONObject object = JSONObject. fromObject (result); 41 // Log. e ("MyLog", "==== object ===" + object); 42 // JSONObject ob = object. getJSONObject ("result"); 43 // JSONArray ob1 = ob. getJSONArray ("data"); 44 // JSONObject o2 = ob1.getJSONObject (0); 45 // JSONArray po = o2.getJSONArray ("steps "); 46 // 47 // if (object. getInt ("error_code") = 0) {48 // title = o2.getString ("title"); 49 // ingre Dient = o2.getString ("ingredients"); 50 // burden = o2.getString ("burden"); 51 // for (int I = 0; I <po. size (); I ++) {52 // 53 // JSONObject u = po. getJSONObject (I); 54 // 55 // B [I] = (u. getString ("step"); 56 // 57 //} 58 // mu. setTitle (title); 59 // mu. setIngredient (ingredient); 60 // mu. setBurden (burden); 61 // mu. setB (B); 62 // 63 //} else {64 // System. out. println (object. get ("error_code") + ":" + object. get ("Reason"); 65 // 66 //} 67 68} catch (Exception e) {69 e. printStackTrace (); 70 Log. e ("MyLog", "====+ error_code ===" + e. getMessage (); 71} 72 return mu; 73} 74 75/** 76*77 * @ param strUrl request address 78 * @ param params request parameter 79 * @ param method Request method 80 * @ return network request string 81 *@ throws Exception 82 */83 public static String net (String strUrl, map params, String method) throws Exception {84 HttpURLConne Ction conn = null; 85 BufferedReader reader = null; 86 String rs = null; 87 try {88 StringBuffer sb = new StringBuffer (); 89 if (method = null | method. equals ("GET") {90 strUrl = strUrl + "? "+ Urlencode (params); 91} 92 Log. e ("MyLog", "---- url ------" + strUrl); 93 URL url = new URL (strUrl); 94 conn = (HttpURLConnection) url. openConnection (); 95 if (method = null | method. equals ("GET") {96 conn. setRequestMethod ("GET"); 97} else {98 conn. setRequestMethod ("POST"); 99 conn. setDoOutput (true); 100} 101 conn. setRequestProperty ("User-agent", userAgent); 102 conn. setUseCaches (false); 103 conn. setConn EctTimeout (DEF_CONN_TIMEOUT); 104 conn. setReadTimeout (DEF_READ_TIMEOUT); 105 conn. setInstanceFollowRedirects (false); 106 conn. connect (); 107 if (params! = Null & method. equals ("POST") {108 try {109 DataOutputStream out = new DataOutputStream (conn. getOutputStream (); 110 out. writeBytes (urlencode (params); 111} catch (Exception e) {112 Log. e ("MyLog", "---- e ------" + e. getMessage (); 113} 114} 115 InputStream is = conn. getInputStream (); 116 rs = read (is); 117 // reader = new BufferedReader (new InputStreamReader (is); 118 // String strRead = null; 119 // wh Ile (strRead = reader. readLine ())! = Null) {120 // sb. append (strRead); 121 //} 122 // rs = sb. toString (); 123 Log. e ("MyLog", "---- rs ------" + rs); 124} catch (IOException e) {125 Log. e ("MyLog", "---- e ------" + e. getMessage (); 126 e. printStackTrace (); 127} finally {128 if (reader! = Null) {129 reader. close (); 130} 131 if (conn! = Null) {132 conn. disconnect (); 133} 134} 135 return rs; 136} 137 138 private static String read (InputStream is) throws IOException {139 StringBuilder builder = new StringBuilder (); 140 int byteCount = 0; 141 byte [] bytes = new byte [1024]; 142 while (byteCount = is. read (bytes ))! =-1) {143 String text = new String (bytes, 0, byteCount); 144 Log. e ("MyLog", "---- ========------" + text); 145 builder. append (text); 146} 147 Log. e ("MyLog ","----??????????? ------ "+ Builder. toString (); 148 return builder. toString (); 149} 150 151 // convert map type to request parameter type 152 public static String urlencode (Map <String, Object> data) {153 StringBuilder sb = new StringBuilder (); 154 for (Map. entry I: data. entrySet () {155 try {156 sb. append (I. getKey ()). append ("= "). append (URLEncoder. encode (I. getValue () + "", "UTF-8 ")). append ("&"); 157} catch (UnsupportedEncodingException e) {158 e. printStackTrace (); 159} 160} 161 return sb. toString (); 162} 163}
The problems I encountered at the beginning were: I initially used eclipse to call the interface for testing, and there was no problem at all. Then I put that part of the code back to the AS, and there was a problem. The data was always not fully obtained. Then I changed the json parsing file. (the above comment is the previous JSon parsing code)
1 public class MainActivity extends AppCompatActivity {// main interface 2 private EditText med; 3 private Button mb; 4 private List <Food> foodlist = new ArrayList <> (); 5 6 @ Override 7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_main); 10 initFoods (); 11 FoodAdapter adapter = new FoodAdapter (MainActivity. this, R. layout. food, foodlist); 12 med = findViewById (R. id. edit_text); 13 mb = findViewById (R. id. button); 14 mb. setOnClickListener (new View. onClickListener () {15 @ Override16 public void onClick (View view) {17 String num = med. getText (). toString (); 18 Intent in = new Intent (MainActivity. this, DetailActivity. class); 19 in. putExtra ("med", num); 20 startActivity (in); 21} 22}); 23 EditText editText = (EditText) findViewById (R. id. edit_text); 24 25 ListView listView = (ListView) findViewById (R. id. list_view); 26 listView. setAdapter (adapter); 27} 28 private void initFoods () {29 Food a = new Food (R. drawable. food1); 30 foodlist. add (a); 31 Food B = new Food (R. drawable. food2); 32 foodlist. add (B); 33 Food c = new Food (R. drawable. food3); 34 foodlist. add (c); 35 36} 37}
Public class DetailActivity extends AppCompatActivity {// display page private TextView TV, tv2, tv3, tv4; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_detail); TV = findViewById (R. id. tvTitle); tv2 = findViewById (R. id. tvIngredients); tv3 = findViewById (R. id. tvBunder); tv4 = findViewById (R. id. tvSteps); Intent f = getIntent (); String ji = f. getStringExtra ("med"); new networktask(cmd.exe cute (ji);} private class NetworkTask extends AsyncTask <String, Void, menubean >{@ Override protected menubean doInBackground (String... ji) {Juhedemo nu2 = new Juhedemo (); menubean kk = nu2.getRequest4 (ji [0]); Log. e ("MyLog", "=======" + kk); // String title2 = kk. getTitle (); return kk ;}@ Override protected void onPostExecute (menubean kk) {ArrayList <String> ad = new ArrayList <String> (); String dg = ""; ad = kk. getB (); for (int I = 0; I <ad. size (); I ++) {dg = dg + ad. get (I);} super. onPostExecute (kk); TV. setText (kk. getTitle (); tv2.setText (kk. getIngredient (); tv3.setText (kk. getBurden (); tv4.setText (dg );}}}
This is coming to an end. Continue to work! (If you want to know what the lucky number corresponds to, leave a message and leave a number for you)