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>
    
  • 相关阅读:
    曾经拥有,今生无悔
    WinRAR 4.20 beta2 key!注册文件 注册码
    加谁的QQ,并聊天‘
    自己读c
    字符串和字符数组做函数参数是的区别,
    数组
    *p和++对p的影响和对*p的影响
    字符串赋给指针
    memset函数,还没看2013.12.19
    strtock函数小结。
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781932.html
Copyright © 2011-2022 走看看