使用GSON
来源:互联网 发布:模拟器mac版 编辑:程序博客网 时间:2024/06/16 16:37
运行截图
App.java
package csdn.example.com.notification.NetWorkTest;/** * Created by 王德强 on 2017/7/24. */public class App { private String id; private String name; private String version; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; }}
MainActivity.java
package csdn.example.com.notification.MyApp;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;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 java.util.List;import csdn.example.com.notification.NetWorkTest.App;import csdn.example.com.notification.R;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class Main7Activity extends AppCompatActivity implements View.OnClickListener { TextView requesetText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main7); requesetText = (TextView) findViewById(R.id.request_text); Button send_request = (Button) findViewById(R.id.send_request); send_request.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() { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://10.7.90.163:8088/get_data.json") .build(); Response response = client.newCall(request).execute(); String requestData = response.body().string(); //showResponse(requestData); parseJSONWithGSON(requestData); } catch (Exception e) { e.printStackTrace(); } } }).start(); } private void parseJSONWithGSON(String requestData) { Gson gson = new Gson(); List<App> appList = gson.fromJson(requestData,new TypeToken<List<App>>(){}.getType()); for (App app : appList){ Log.i("Main7","name is "+app.getName()); Log.i("Main7","version is "+app.getVersion()); Log.i("Main7","id is "+app.getId()); } } private void showResponse(final String requestData) { runOnUiThread(new Runnable() { @Override public void run() { requesetText.setText(requestData); } }); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/send_request" android:text="发送请求" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/request_text" /> </ScrollView></LinearLayout>
阅读全文
0 0
- 【Gson】Gson 使用笔记
- gson使用
- Gson 使用
- gson 使用
- Gson使用
- Gson使用
- gson使用
- Gson 使用
- gson使用
- Gson 使用
- Gson使用
- GSON使用
- Gson使用
- Gson使用
- Gson使用
- Gson 使用
- gson使用
- Gson使用
- ubuntu软件操作命令
- 拓扑排序入门
- 想提高团队技术,来试试这个套路!
- 剑指offer:替换空格
- EL表达式取Map,List值的总结
- 使用GSON
- string和stringstream+setw()用法总结
- dialog级联写法
- CSU-ACM2017暑期训练1-Debug与STL C
- MySharedpreference
- 冲突域和广播域的区分
- HTML基础汇总
- 31. Next Permutation
- 用IDEA开发简单的Servlet