httpclientpost网络获取数据

来源:互联网 发布:淘宝店铺怎么上众划算 编辑:程序博客网 时间:2024/06/06 20:00
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnRead).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        getMenu("ed1088c0c4ac34c42a270fabf0de9b5a");
                    }
                }.start();
            }
        });
    }
    private void getMenu(String key){
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost("http://v.juhe.cn/WNXG/city");
        List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("key",key));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(list));
            HttpResponse httpResponse=httpClient.execute(httpPost);
            int code = httpResponse.getStatusLine().getStatusCode();
            if (code== HttpStatus.SC_OK){
                InputStream is = httpResponse.getEntity().getContent();
                String str = StreamTools.transForm(is);
                Log.w("=====", str);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
0 0
原创粉丝点击