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("代码出错"); }
}
}