zoukankan      html  css  js  c++  java
  • 使用GSON

    运行截图

    Logcat
    Genymotion
    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>
    
  • 相关阅读:
    168. Excel Sheet Column Title
    461. Hamming Distance
    Tree Representation Implementation & Traversal
    404. Sum of Left Leaves
    572. Subtree of Another Tree
    20. Valid Parentheses
    Check time of different search methods
    Binary search tree or not
    Coin Change
    JS DOM:文档对象模型 --树模型 文档:标签文档,对象:文档中每个元素对象,模型:抽象化的东西
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781931.html
Copyright © 2011-2022 走看看