android http 连接通信
2012-12-11 16:19
281 查看
一共有三个文件,J***A,MAIN.XML,AndroidManifest.xml
J***A文件:
J***A文件:
package Android_https.com; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; 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.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Android_httpsActivity extends Activity { privateButton sendbutton = null; privateHttpResponse httpResponse = null; privateHttpEntity httpEntity = null; @Override public voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendbutton = (Button)findViewById(R.id.sendbutton); sendbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { HttpGet httpget = newHttpGet("http://localhost/"); HttpClient httpClient = newDefaultHttpClient(); InputStream inputStream = null; try { httpResponse = httpClient.execute(httpget); httpEntity = httpResponse.getEntity(); inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(newInputStreamReader(inputStream)); String result = "" ; String line = ""; while ((line = reader.readLine()) != null){ result = result + line; } System.out.println(result); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("有错误!!!"); e.printStackTrace(); } } }); } }
<?xml version="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/sendbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="简单发送请求,并回得报文" /> </LinearLayout>
<?xml version="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="Android_https.com" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="3"/> <uses-permissionandroid:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".Android_httpsActivity" > <intent-filter > <action android:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- android HTTP通信方式连接网络
- android http 连接通信
- android http通信方式连接网络
- android http通信方式连接网络
- android http通信方式连接网络
- Android在Http连接中设置超时和代理?
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- Android之Http通信——5.开发中遇到的一些问题
- Android网络连接,HttpsURLConnection还是ApacheHTTPClient?
- Android网络编程之Http通信 推荐
- Android网络连接的两种方法:apache client和httpurlconnection的比较
- Androin学习笔记四十二:Java android Socket通信检测(server)连接是否断开
- Android网络连接之HttpURLConnection和HttpClient
- 系出名门Android(10) - HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理
- Android 中判断网络连接和GPS是否可用及HTTPCLIENT使用
- Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
- Android开发之Http通信HttpURLConnection接口
- android和jsp servlet的通信 http协议
- Android HTTP通信协议
- Android网络连接之HttpURLConnection和HttpClient