HttpClient接口Get方式

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class mainActivity extends Activity {
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv1);
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet("http://10.0.2.2:8080/dem/");
        try {
            // 取得HttpClient对象
            HttpClient httpclient = new DefaultHttpClient();
            // 请求HttpClient取得HttpResponse
            HttpResponse httpResponse = httpclient.execute(httpRequest);
            // 请求成功
            if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String strResult = EntityUtils.toString(httpResponse
                        .getEntity());
                tv.setText(strResult);
            }
            else {
                tv.setText("请求失败");
            }
        }
        catch(Exception e) {
            Log.e("", e.getMessage());
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(exception,String,Class)