package com.example.xiayujia20151107;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import com.example.vo.Cai;
import com.example.vo.CaiDanVo;
import com.example.vo.ShangCheng;
import com.example.vo.XinJsonVo;
import com.google.gson.Gson;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MyAsy2 extends AsyncTask<String, Integer, List<Cai>> {
private Context context;
private ListView lv;
private TextView tv;
public MyAsy2(Context context, ListView lv, TextView tv) {
super();
this.context = context;
this.lv = lv;
this.tv = tv;
}
@Override
protected List<Cai> doInBackground(String... arg0) {
try {
URL url=new URL(arg0[0]);
System.out.println(arg0[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// connection.setDoOutput(true);
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestProperty("Charset","UTF-8");
// OutputStream os = connection.getOutputStream();
// PrintWriter pw = new PrintWriter(os);
// pw.print("key=3041a2ebdcc44d5cdb886cd7b4f05884");
// pw.print("date=2015-1-1&key=98bb583aa1b1fcf4b2bc331416387ee2");
// pw.flush();
// pw.close();
InputStream is=connection.getInputStream();
StringBuffer buff=new StringBuffer();
byte[] b=new byte[1024];
int leng=0;
while((leng=is.read(b))>0){
buff.append(new String(b,0,leng));
}
is.close();
String sjson=buff.toString();
Gson gson=new Gson();
CaiDanVo js=gson.fromJson(sjson, CaiDanVo.class);
List<Cai> list=js.getResult().getData();
System.out.println(list);
return list;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(java.util.List<Cai> result) {
BaseAdapter ba=new Myadapter2(context,result);
lv.setAdapter(ba);
};
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import com.example.vo.Cai;
import com.example.vo.CaiDanVo;
import com.example.vo.ShangCheng;
import com.example.vo.XinJsonVo;
import com.google.gson.Gson;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MyAsy2 extends AsyncTask<String, Integer, List<Cai>> {
private Context context;
private ListView lv;
private TextView tv;
public MyAsy2(Context context, ListView lv, TextView tv) {
super();
this.context = context;
this.lv = lv;
this.tv = tv;
}
@Override
protected List<Cai> doInBackground(String... arg0) {
try {
URL url=new URL(arg0[0]);
System.out.println(arg0[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// connection.setDoOutput(true);
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestProperty("Charset","UTF-8");
// OutputStream os = connection.getOutputStream();
// PrintWriter pw = new PrintWriter(os);
// pw.print("key=3041a2ebdcc44d5cdb886cd7b4f05884");
// pw.print("date=2015-1-1&key=98bb583aa1b1fcf4b2bc331416387ee2");
// pw.flush();
// pw.close();
InputStream is=connection.getInputStream();
StringBuffer buff=new StringBuffer();
byte[] b=new byte[1024];
int leng=0;
while((leng=is.read(b))>0){
buff.append(new String(b,0,leng));
}
is.close();
String sjson=buff.toString();
Gson gson=new Gson();
CaiDanVo js=gson.fromJson(sjson, CaiDanVo.class);
List<Cai> list=js.getResult().getData();
System.out.println(list);
return list;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(java.util.List<Cai> result) {
BaseAdapter ba=new Myadapter2(context,result);
lv.setAdapter(ba);
};
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}