//Mainactivity
package animtest.com.example.e531.week1_09month;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnClient = (Button) findViewById(R.id.btn_client);
Button btnUrl = (Button) findViewById(R.id.btn_url);
listView = (ListView) findViewById(R.id.lv);
progressBar=(ProgressBar) findViewById(R.id.progressBar);
//初使化为 看不见的状态
progressBar.setVisibility(View.INVISIBLE);
btnClient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//执行异步任务
MyTask myTask = new MyTask();
myTask.execute(new String[]{
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10", "1"});
}
});
btnUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//执行异步任务
MyTask myTask = new MyTask();
myTask.execute(new String[]{
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10", "2"});
}
});
}
class MyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String resultStr = "";
//得到请求的类型
String type = params[1];