android post(HTTP设置参数,仿html表单提交)
package com.example.testhttpurlcon; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Thread td = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub httpUrlConTest huc = new httpUrlConTest(); huc.testHttpUrlCon(); } }); td.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
package com.example.testhttpurlcon; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.util.Log; public class httpUrlConTest { public void testHttpUrlCon() { try { String url ="http://www.xxxx.com:端口号/xxx"; // 创建连接 HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); // 设置参数,仿html表单提交 List<NameValuePair> paramList = new ArrayList<NameValuePair>(); BasicNameValuePair param1 = new BasicNameValuePair("name", "xxx"); BasicNameValuePair param2 = new BasicNameValuePair("pass", "xxx"); paramList.add(param1);paramList.add(param2); post.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8)); // 发送HttpPost请求,并返回HttpResponse对象 HttpResponse httpResponse = httpClient.execute(post); // 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 获取返回结果 String result = EntityUtils.toString(httpResponse .getEntity()); System.out.println(result); Log.i("sm", result); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testhttpurlcon" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <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.testhttpurlcon.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> </manifest>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。