package com.example.android;

import java.io.IOException; import java.util.HashMap;

import android.app.Activity; import android.content.Intent;

import android.os.Bundle; import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button; import com.google.api.client.http.GenericUrl;

import com.google.api.client.http.HttpRequest;

import com.google.api.client.http.HttpRequestFactory;

import com.google.api.client.http.HttpRequestInitializer;

import com.google.api.client.http.HttpResponse;

import com.google.api.client.http.HttpTransport;

import com.google.api.client.http.UrlEncodedContent;

import com.google.api.client.http.javanet.NetHttpTransport;

import com.google.api.client.json.JsonFactory;

import com.google.api.client.json.JsonObjectParser;

import com.google.api.client.json.jackson.JacksonFactory;

import com.google.api.client.util.Key;

public class MainActivity extends Activity {

public static Result result =null;

public static class Result {

@Key("Code")

private int code;

@Key("Data")

private ProductData data;

public int getCode() {

return code;   }

public ProductData getData() {

return data;

}

}

public static class ProductData {

@Key("Total")

private int total;

@Key("List")

private Product[] list;

public int getTotal() {

return total;   }

public Product[] getList() {

return list;

}

}

public static class Product {

@Key("id")

private int id;

@Key("title")

private String title;

public int getId() {

return id;   }

public String getTitle() {

return title;   }  }

Button btn = null;

static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();// For Java: NetHttpTransport(HttpURLConnection ); For Android: AndroidHttp.  static final JsonFactory JSON_FACTORY = new JacksonFactory(); // For JSON converting.

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button)this.findViewById(R.id.btnacq);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

test();

Intent intent = new Intent();

intent.setClass(MainActivity.this, Show.class);

intent.putExtra("code", result.getCode());//向下一个activity传入数值

intent.putExtra("data", result.getData().total);

intent.putExtra("list", result.getData().list);

startActivity(intent);    }   });     }

public static void test() {   HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {

@Override

public void initialize(HttpRequest request) throws IOException {     request.setParser(new JsonObjectParser(JSON_FACTORY));    }       });

GenericUrl url = new GenericUrl("url"); // ps=20&pi=0&t=8&ti=1&catetag=[]&custag=[]&favtag=[]&name=

HashMap<String, String> params = new HashMap<String, String>();

params.put("ps", "20");//ajax代码写的   params.put("pi", "0");

params.put("t", "8");   params.put("ti", "1");   params.put("catetag", "[]");

params.put("custag", "[]");   params.put("favtag", "[]");   params.put("name", "");

try {    HttpRequest request = requestFactory.buildPostRequest(url, new UrlEncodedContent(params));//请用post请求

HttpResponse response = request.execute();

result = response.parseAs(Result.class);//根据自己定义的的对象的get方法获取里面的值

System.out.println(result);

System.out.println(result.getCode()+"-------"+result.getData());    System.out.println(result.getData().total+"----"+result.getData().list);   }

catch (IOException e) {    System.out.println("代码出错");   }

}

}

google-http-java-client(android学习篇)的更多相关文章

  1. google-http-java-client(android学习篇2源码)

    package com.google.api.services.samples.googleplus.cmdline.simple;   import com.google.api.client.ht ...

  2. Java for Android 学习第一周

    前言 专业Java程序员所必需掌握的3个主题: 1. Java编程语言 2. 使用Java的面向对象编程(OOP) 3. Java核心库 JDK.JRE和JVM 1. javac编译java源代码为字 ...

  3. java+android学习路线图

    java.android学习路线图  看图之前先按住Ctrl键同时滑动鼠标滚轮

  4. Java多线程(学习篇)

    Java多线程:(学习篇) 1.什么是线程 2.线程状态 3.线程中断 4.线程交互 5.同步机制 6.锁机制 7.堵塞队列与堵塞栈 8.条件变量.原子量.线程池等 9.线性安全类和Callable与 ...

  5. Android学习系列(37)--App调试内存泄露之Context篇(下)

    接着<Android学习系列(36)--App调试内存泄露之Context篇(上)>继续分析. 5. AsyncTask对象 我N年前去盛大面过一次试,当时面试官极力推荐我使用AsyncT ...

  6. android基础篇学习心得

    android技术中,线程.进程.JNI.IPC和各个小框架结构是基本功.在跟随高焕堂老师的android程序猿到架构师之路系列视频中 学习完基础篇之后,颇有些心得,记录下来. android开发就是 ...

  7. 【朝花夕拾】Android性能篇之(三)Java内存回收

    在上一篇日志([朝花夕拾]Android性能篇之(二)Java内存分配)中有讲到,JVM内存由程序计数器.虚拟机栈.本地方法栈.GC堆,方法区五个部分组成.其中GC堆是一块多线程的共享区域,它存在的作 ...

  8. 漫谈 Google 的 Native Client 技术(一)---- 历史动力篇(Web 本地计算发展史)

    转自:http://hzx5.blog.163.com/blog/static/40744388201172522313463/ 漫谈 Google 的 Native Client 技术(一)---- ...

  9. 四、Android学习第四天——JAVA基础回顾(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 四.Android学习第四天——JAVA基础回顾 这才学习Android的 ...

随机推荐

  1. JS内置对象

    字符串对象 <script> //字符串对象 var str = "Hello worldlsgjlsjg"; document.write('string.lengt ...

  2. Python学习路程day5

    冒泡排序 将一个不规则的数组按从小到大的顺序进行排序 data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6] #第一次循环,最后一个数字不需要循环,因为最大值已经 ...

  3. Gradle: The New Android Build System

    Gradle: The New Android Build System Google selected Gradle as the foundation of the Android SDK bui ...

  4. HackRF实现GPS欺骗教程

    硬件平台:HackRF One软件平台:MAC运行环境搭建系统平台:OS X 10.11 EI CapitanGPS终端:One Plus手机,飞行模式,仅GPS定位,GPS test App文章特点 ...

  5. PP

  6. Android中Preference的使用以及监听事件分析

    在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...

  7. C++ algorithm 里的sort函数应用

    MSDN中的定义: template<class RanIt>    void sort(RanIt first, RanIt last); //--> 1)template< ...

  8. Functions

    Small The first rule of functions is that they should be small.The second rule of functions is that ...

  9. 步步入佳境---UI入门(4) --简单练习

    一,创建SingleViewApplication 1,UILabel的简单使用 UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, ...

  10. postfix之dovecot

    dovecot-1.0.rc14安装 注:我的系统是RHEL4一.RPM格式1.安装RPM包:2.编辑/etc/dovecot.conf其中,修改protocols = imap pop3 passd ...