Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
来源:互联网 发布:单片机串口通信程序 编辑:程序博客网 时间:2024/06/03 21:35
activaty_login.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.sj_xml.LoginActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="admin" android:id="@+id/et_main_uname" /> <EditText android:id="@+id/et_main_upass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="123456" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByGet" android:text="登录(GET)" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByPost" android:text="登录(POST)" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByAsyncHttpClient" android:text="登录(AsyncHttpClient)" /></LinearLayout>
LoginActivaty.Java
package com.example.sj_xml;import android.os.AsyncTask;import android.preference.PreferenceActivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.RequestParams;import com.loopj.android.http.TextHttpResponseHandler;import org.apache.http.Header;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class LoginActivity extends AppCompatActivity { private EditText et_main_uname; private EditText et_main_upass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); et_main_uname = (EditText) findViewById(R.id.et_main_uname); et_main_upass = (EditText) findViewById(R.id.et_main_upass); } public void loginByGet(View view) { String uname = et_main_uname.getText().toString(); String upass = et_main_upass.getText().toString(); String path = "http://192.168.253.1:8090/front/getlogin.xhtml"; //可变数组 new MyTask().execute(uname, upass, path, "GET"); } public void loginByPost(View view) { String uname = et_main_uname.getText().toString(); String upass = et_main_upass.getText().toString(); String path = "http://192.168.253.1:8090/front/getlogin.xhtml"; //可变数组 new MyTask().execute(uname, upass, path, "POST"); } public void loginByAsyncHttpClient(View view) { String uname = et_main_uname.getText().toString(); String upass = et_main_upass.getText().toString(); String path = "http://192.168.253.1:8090/front/getlogin.xhtml"; AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); RequestParams requestParams=new RequestParams(); requestParams.put("uname",uname); requestParams.put("upwd",upass); asyncHttpClient.post(path,requestParams,new TextHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, String responseBody) { super.onSuccess(statusCode, headers, responseBody); Toast.makeText(LoginActivity.this, responseBody, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) { super.onFailure(statusCode, headers, responseBody, error); } }); } class MyTask extends AsyncTask { private HttpURLConnection connection; private URL url; @Override protected Object doInBackground(Object[] objects) { //获取参数的值 String uname = objects[0].toString(); String upass = objects[1].toString(); String path = objects[2].toString(); String type = objects[3].toString(); String str = "uname=" + uname + "&upwd=" + upass; try { if ("GET".equals(type)) { //用GET方式提交 path = path + "?" + str; url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); } else if ("POST".equals(type)) { //用POST方式提交 url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); //设置contentType contentLength connection.setRequestProperty("Content-Length", str.length() + ""); connection.setRequestProperty("Content-Type", "text/plain;charset=UTF-8"); //设置允许对外输出数据 connection.setDoOutput(true); //将用户名和密码提交到服务器 connection.getOutputStream().write(str.getBytes()); } connection.setConnectTimeout(5000); if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String result = br.readLine(); return result; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); String s = (String) o; Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show(); } }}
0 0
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android向服务器提交数据(方式:get、post、AsyncHttpClient )
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- Android Get,Post,AsyncHttpClient向服务器提交数据三种方式
- Android-使用GET、POST、AsyncHttpClient向服务器提交数据
- android 向服务器提交数据(get-post-AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android之向服务器提交数据(POST,GET,AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android-向服务器提交数据(GET-POST-AsyncHttpClient)
- android之向服务器提交数据(GET,POST,AsyncHttpClient)
- 向服务器提交数据(GET-POST-AsyncHttpClient)
- 安卓 向服务器提交数据(方式:get、post、AsyncHttpClient )
- 32.Android端向web端(服务器)提交数据(GET-POST-AsyncHttpClient)
- android中用get和post方式向服务器提交请求
- Android之使用HTTP协议的Get/Post方式向服务器提交数据
- Android之使用HTTP协议的Get/Post方式向服务器提交数据
- Android 深入(一)- 自定义View之BottomTitleImageView
- 如何利用yum的缓存文件
- cocos2d-x3.X 在Windows下打包Android平台APK
- mAP的一点理解
- OVM 免费虚拟化软件迭代时间调整,提高产品稳定性!
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- redis入门——redis常用命令
- 超详细:JAVA常用的设计模式汇总
- vs中检测内存泄漏的方法
- android developer tiny share-20170222
- Mac 下编辑Ysdk的坑
- linux多线程-----条件变量、pthread_cond_xxx
- Executors,ExecutorService线程知识
- Android面试题收集