JSON

Android开发学习之路-网络编程之xml、json(3)

字号+ 作者:H5之家 来源:H5之家 2017-06-13 13:03 我要评论( )

如图可知配置已经ok了,那么接下来就开始完成代码了,这里要使用JSONObject和GSON来实现,布局如下: ?xml version=1.0 encoding=utf-8?LinearLayoutxmlns:android=xmlns:tools=android:layout_width=match_parenta

    如图可知配置已经ok了,那么接下来就开始完成代码了,这里要使用JSONObject和GSON来实现,布局如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="10dp" tools:context="com.jared.emjsonstudy.MainActivity"> <Button android:id="@+id/getJSONObject" android:text="Get Json With JSONObject" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false"/> <Button android:id="@+id/getGSON" android:text="Get Json With GSON" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false"/> </LinearLayout>
    接着实现代码,还是用了Async-HttpClient来实现,代码如下: package com.jared.emjsonstudy; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import org.json.JSONArray; import org.json.JSONObject; import cz.msebera.android.httpclient.Header; public class MainActivity extends AppCompatActivity { private static final String TAB = "JSONStudy"; private static final String JSON_URL = ""; private Button mGetJSONObjectBtn; private Button mGetGSONBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGetJSONObjectBtn = (Button)findViewById(R.id.getJSONObject); mGetGSONBtn = (Button)findViewById(R.id.getGSON); mGetJSONObjectBtn.setOnClickListener(new myOnClickListener()); mGetGSONBtn.setOnClickListener(new myOnClickListener()); } private class myOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.getJSONObject: getJSONWithJSONObject(JSON_URL); break; case R.id.getGSON: break; default: break; } } } private void parseJSONWithJSONObject(String jsonData) { try { JSONArray jsonArray = new JSONArray(jsonData); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String mName = jsonObject.getString("name"); String mAge = jsonObject.getString("age"); String mSex = jsonObject.getString("sex"); Log.d(TAB, "name is: " + mName); Log.d(TAB, "age is: " + mAge); Log.d(TAB, "sex is:" + mSex); } } catch (Exception e) { e.printStackTrace(); } } private void getJSONWithJSONObject(String url) { AsyncHttpClient client = new AsyncHttpClient(); client.get(url, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { try { String response = new String(bytes, 0, bytes.length, "utf-8"); parseJSONWithJSONObject(response); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { } }); } }
    如上代码明显比xml的简单多了,JSONArray获取到Json数据,然后通过JSONObject来获取对应键值的内容。因为我的电脑ip地址是192.168.1.102,手机和电脑在同一个网段,所以直接利用真机来测试,效果如下: 02-20 10:13:20.521 4947-4947/? D/JSONStudy: name is: xiao hong 02-20 10:13:20.521 4947-4947/? D/JSONStudy: age is: 25 02-20 10:13:20.521 4947-4947/? D/JSONStudy: sex is:wonan 02-20 10:13:20.521 4947-4947/? D/JSONStudy: name is: xiao ming 02-20 10:13:20.521 4947-4947/? D/JSONStudy: age is: 15 02-20 10:13:20.521 4947-4947/? D/JSONStudy: sex is:man 02-20 10:13:20.521 4947-4947/? D/JSONStudy: name is: xiao qiang 02-20 10:13:20.521 4947-4947/? D/JSONStudy: age is: 30 02-20 10:13:20.521 4947-4947/? D/JSONStudy: sex is:man    

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评
o