android网络编程(四)HttpClient GET

本文展示了如何利用Android中的HttpClient类实现GET请求,包括构造HttpGet对象、获取HttpClient实例、执行请求并处理响应结果的过程。代码示例中包含了HTTP请求URL、请求发送与接收的完整流程以及错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0070/2043/788f9f81-bd71-3063-be55-bef0c5d73ab4.jpg[/img]
[/img]

package com.amaker.http3;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView tv;
private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
requestByGet();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public String requestByGet() throws Exception {
String strResult = "";
// http地址
String httpUrl = "http://open.ifenghui.cn/mobilecms/interface/mobile.action?code=888032100100&m=get_coolimages&p=1&filter=all";
// HttpGet连接对象
HttpGet httpRequest = new HttpGet(httpUrl);
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
strResult = EntityUtils.toString(httpResponse.getEntity());
tv.setText(strResult);
} else {
tv.setText("请求错误!");
}
return strResult;
}

}



main.xm:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_x="0px"
android:layout_y="20px">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="@string/hello" />
</ScrollView>
<Button
android:text="HttpClient-GET"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.http3"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!--不要忘记设置网络访问权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值