Android HTTP send request and receive response instance code _android

Source: Internet
Author: User
Add Permissions
First, add access to the network in the manifest:
Copy Code code as follows:

<manifest ... >
<uses-permission android:name= "Android.permission.INTERNET"/>
...
</manifest>

The complete manifest file is as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.httpdemo1"
Android:versioncode= "1"
Android:versionname= "1.0" >
<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"
<uses-permission android:name= "Android.permission.INTERNET"/>
<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Com.example.httpdemo1.HttpDemo1Activity"
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>
</manifest>

The layout code is as follows:
Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Httpdemo1activity ">
<textview
Android:id= "@+id/mywebtitle"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= ""/>
<button
Android:id= "@+id/requestbtn"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
android:text= "Send Request"/>
<webview
Android:id= "@+id/webview"
Android:layout_width= "Fill_parent"
android:layout_height= "Match_parent"
Android:layout_above= "@id/requestbtn"
android:layout_below= "@id/mywebtitle"/>
</RelativeLayout>
Activity_http_demo1.xml

The main code:
Copy Code code as follows:

Package com.example.httpdemo1;
Import Java.io.BufferedReader;
Import java.io.IOException;
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.os.Bundle;
Import android.app.Activity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.webkit.WebView;
Import Android.widget.Button;
public class Httpdemo1activity extends activity
{
Private button MSENDREQBTN = null;//Send requested button
Private WebView Mwebview = null;//for displaying results, displaying response results in a loaded HTML string instead of loading URLs in WebView own way
Response
Private HttpResponse mhttpresponse = null;
Entity
Private httpentity mhttpentity = null;
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_HTTP_DEMO1);
MSENDREQBTN = (Button) Findviewbyid (R.ID.REQUESTBTN);
Msendreqbtn.setonclicklistener (Msendclicklistener);
Mwebview = (webview) Findviewbyid (R.id.webview);
}
Private Onclicklistener Msendclicklistener = new Onclicklistener ()
{
@Override
public void OnClick (View v)
{
Generate a Request object
HttpGet httpget = new HttpGet ("http://www.baidu.com/");
Generating an HTTP client object
HttpClient httpclient = new Defaulthttpclient ();
The following HTTP client is used to send the request and get the response content
InputStream inputstream = null;
Try
{
Send request and get response object
Mhttpresponse = Httpclient.execute (HttpGet);
The message entity that received the response
Mhttpentity = Mhttpresponse.getentity ();
Gets an input stream
InputStream = Mhttpentity.getcontent ();
BufferedReader BufferedReader = new BufferedReader (
New InputStreamReader (InputStream));
String result = "";
String line = "";
while (null!= (line = Bufferedreader.readline ()))
{
result + = line;
}
Print the results, and you can view them in Logcat
SYSTEM.OUT.PRINTLN (result);
Load content into WebView display
Mwebview.getsettings (). Setdefaulttextencodingname ("UTF-8");
Direct use of Mwebview.loaddata (result, "text/html", "Utf-8"); Will show web page not found
Replace the following way to normal display (but relatively wide, drag visible Baidu logo)
Mwebview.loaddatawithbaseurl (NULL, result, "text/html",
"Utf-8", null);
Loading URLs directly can also display the page (but this example is primarily to verify that the response returned by the string is correct, so do not use the following line of code)
Mwebview.loadurl ("http://www.baidu.com/");
}
catch (Exception e)
{
E.printstacktrace ();
}
Finally
{
Try
{
Inputstream.close ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}
};
}

The results of the program operation are as follows:

reference materials
Android Development Video Tutorial HTTP operation. --http://www.marsdroid.org
Android reference:package org.apache.http:
Http://developer.android.com/reference/org/apache/http/package-summary.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.