gson的安装和使用
gson的安装和使用
1.安装
2.布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/response_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
3.查看声明
apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "demo.jq.com.networktest" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.google.code.gson:gson:2.8.2' }
4.创建接收数据的类
package demo.jq.com.networktest; import java.lang.reflect.Array; /** * @author jim */ public class ApiData { String[] data =new String[5]; private Integer code; private String message; public String[] getData() { return data; } public void setData(String[] data) { this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
5.主体动作
package demo.jq.com.networktest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * @author jim */ public class MainActivity extends AppCompatActivity implements View.OnClickListener{ TextView responseText; private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendRequest = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response_text); sendRequest.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.send_request) { sendRequestWithOkHttp(); } } private void sendRequestWithOkHttp() { // 开启线程来发起网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; BufferedReader reader = null; try { OkHttpClient client = new OkHttpClient(); // post请求 RequestBody requestBody = new FormBody.Builder() .add("type","1") .build(); Request request = new Request.Builder() .url("http://devmg.yunlutong.com/api/test/testApi") .post(requestBody) .build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); // 解析json数据 parseJSONWithGSON(responseData); showResponse(responseData); } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect(); } } } }).start(); } /** * 解析json * @param jsonData */ private void parseJSONWithGSON(String jsonData) { Gson gson = new Gson(); ApiData apiData = gson.fromJson(jsonData, ApiData.class); Log.d(TAG,"code is" + apiData.getCode()); Log.d(TAG,"message is" + apiData.getMessage()); // 遍历数据 for(int i = 0; i < apiData.getData().length; i++){ Log.d(TAG,"data "+i+" is " + apiData.getData()[i]); } } private void showResponse(final String response) { runOnUiThread(new Runnable() { @Override public void run() { responseText.setText(response); } }); } }
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7714921.html,如需转载请自行联系原作者
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
iOS itunesconnect 权限分配(发布版本前奏)
进入:itunesconnect.apple.com登陆对应的账号,登陆成功之后,有权限、无权限页面分别如下 登陆主账号,点击 填写邀请人的基本信息,APP ID需要要正确的,邀请成功之后,有邮件提示,最后需要点击邮寄链接激活 邀请人对应的权限,选择对应权限之后,有什么功能可以使用,列举在下面了 邮件提示 点击邮件里“activate your account” OK,重新登录一下账号
- 下一篇
全面总结侧滑菜单的5种实现方式
现在开发中,很多APP都在用侧滑菜单,这个很常见,也有很多第三方库,比如slidingmenu,谷歌官方的DrawerLayout等等。下面我就总结一下侧滑菜单实现的4种方式: 一、自定义ViewGroup ,处理其onTouch事件 二、FrameLayout + 手势处理类GestureDetector 三、使用Google自带的DrawerLayout + NavigationView 这个就很简单了,DrawerLayout 结合NavigationView 很轻易实现这个效果。as自带模板就有这个效果。 效果图如下: NavigationView演示.gif 请看代码,具体的我也不多说了: import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2全家桶,快速入门学习开发网站教程
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- MySQL8.0.19开启GTID主从同步CentOS8