JSON

android¿ª·¢ÊµÏÖÌìÆøÇé¿öµÄ»ñÈ¡£¨JsonÀ´·µ»ØÊý¾Ý£©

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-11-07 12:00 ÎÒÒªÆÀÂÛ( )

android¿ª·¢ÊµÏÖÌìÆøÇé¿öµÄ»ñÈ¡£¨JsonÀ´·µ»ØÊý¾Ý£©£¬ÔÚÊäÈë¿òÊäÈë²éѯµÄ³ÇÊУ¬¼´¿ÉÊä³öÌìÆøÇé¿ö¡£Ò»¡¢android¹¤³Ì½á¹¹£º¡¢MainActivity.java:package com.lcz

android¿ª·¢ÊµÏÖÌìÆøÇé¿öµÄ»ñÈ¡£¨JsonÀ´·µ»ØÊý¾Ý£©£¬ÔÚÊäÈë¿òÊäÈë²éѯµÄ³ÇÊУ¬¼´¿ÉÊä³öÌìÆøÇé¿ö¡£

Ò»¡¢android¹¤³Ì½á¹¹£º

\

¡¢

MainActivity.java:

package com.lcz.research; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.lcz.research.utils.StreamTool; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.app.ProgressDialog; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener { private Button btn; private EditText ed_city; private TextView city_result1; private TextView city_result2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button) findViewById(R.id.btn); btn.setOnClickListener(this); ed_city=(EditText) findViewById(R.id.ed_city); city_result1=(TextView) findViewById(R.id.city_result1); city_result2=(TextView) findViewById(R.id.city_result2); } private final static String PATH="https://wthrcdn.etouch.cn/weather_mini?city="; protected static final int SUCCESS = 0; protected static final int INVALID_CITY = 1; protected static final int ERROR = 2; private String city; String ul; private Handler mhandler=new Handler(){ public void handleMessage(android.os.Message msg) { dialog.dismiss(); switch (msg.what) { case SUCCESS: JSONArray data=(JSONArray) msg.obj; try { String day01= data.getString(0); String day02= data.getString(1); city_result1.setText(day01); city_result2.setText(day02); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case INVALID_CITY: Toast.makeText(MainActivity.this, "³ÇÊÐÎÞЧ", 0).show(); break; case ERROR: Toast.makeText(MainActivity.this, "ÍøÂçÎÞЧ", 0).show(); break; default: break; } }; }; ProgressDialog dialog=null; public void onClick(View v) { // TODO Auto-generated method stub city=ed_city.getText().toString().trim(); if(TextUtils.isEmpty(city)){ Toast.makeText(this, "·¾¶´íÎó", 0).show(); return ; } dialog=new ProgressDialog(this); dialog.setMessage("ÕýÔÚÍæÃü¼ÓÔØÖÐ"); dialog.show(); //·¢ÆðÇëÇó¸øÄǸöÍøÕ¾ new Thread(){ public void run() { try { ul=PATH+URLEncoder.encode(city,"UTF-8"); URL url=new URL(ul); //ÉèÖñØÒªµÄ²ÎÊýÐÅÏ¢ HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); //ÅжÏÏìÓ¦Âë int code = conn.getResponseCode(); if(code==200){ //Á¬½ÓÍøÂç³É¹¦ InputStream in = conn.getInputStream(); String data = StreamTool.decodeStream(in); //½âÎöjson¸ñʽµÄÊý¾Ý JSONObject jsonObj=new JSONObject(data); //»ñµÃdescµÄÖµ String result = jsonObj.getString("desc"); if("OK".equals(result)){ //³ÇÊÐÓÐЧ£¬·µ»ØÁËÐèÒªµÄÊý¾Ý JSONObject dataObj = jsonObj.getJSONObject("data"); JSONArray jsonArray = dataObj.getJSONArray("forecast"); //֪ͨ¸üÐÂui Message msg = Message.obtain(); msg.obj=jsonArray; msg.what=SUCCESS; mhandler.sendMessage(msg); }else{ //³ÇÊÐÎÞЧ Message msg=Message.obtain(); msg.what=INVALID_CITY; mhandler.sendMessage(msg); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Message msg = Message.obtain(); msg.what=ERROR; mhandler.sendMessage(msg); } }; }.start(); } }

StreamTool.java:

package com.lcz.research.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTool { public static String decodeStream(InputStream in) throws IOException { // µ×²ãÁ÷ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len =0; byte[] buf = new byte[1024]; while((len=in.read(buf))>0){ baos.write(buf, 0, len); } String data = baos.toString(); return data; } }

2.activity_main.xml

3.

Android

Mainfest.xml

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • AndroidÖйØÓÚJSONÏà¹ØÓ¦Ó÷ÖÎö

    AndroidÖйØÓÚJSONÏà¹ØÓ¦Ó÷ÖÎö

    2017-10-20 18:08

  • Android»ù´¡ÈëÃŽ̡̳ª¡ª7.2.2 Android JSONÊý¾Ý½âÎö

    Android»ù´¡ÈëÃŽ̡̳ª¡ª7.2.2 Android JSONÊý¾Ý½âÎö

    2017-10-19 09:21

  • Android¿ª·¢ÖнØȡijһ¶Îjson×Ö·û´®

    Android¿ª·¢ÖнØȡijһ¶Îjson×Ö·û´®

    2017-10-18 15:33

  • Android±Ê¼Ç£¨ÎåÊ®£© AndroidÖеÄJSONÊý¾Ý

    Android±Ê¼Ç£¨ÎåÊ®£© AndroidÖеÄJSONÊý¾Ý

    2017-10-16 17:00

ÍøÓѵãÆÀ
Ì