首页 > 系统 > Android > 正文

Android 将数据发给服务器

2019-11-07 22:54:42
字体:
来源:转载
供稿:网友

这次一共用三种方式 分别是:原生的GET,原生的POST,和AsyncHttpClient框架。 话不多说,先看运行图 这里写图片描述这里写图片描述 话不多说,看代码 布局:

<?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_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.zking.mysubmitdatatoserver.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:id="@+id/et_main_uname" android:text="admin"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:id="@+id/et_main_upasss" android:text="123456"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginGet" android:text="登陆(GET)"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登陆(POST)" android:onClick="loginPost"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginAnyncHttp" android:text="登陆(AnyncHttp)"/></LinearLayout>

Activity

package com.zking.mysubmitdatatoserver;import android.os.AsyncTask;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 com.zking.utils.HttpParams;import org.apache.http.Header;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class MainActivity extends AppCompatActivity { private EditText et_main_uname; private EditText et_main_upasss; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { et_main_uname = (EditText) findViewById(R.id.et_main_uname); et_main_upasss = (EditText) findViewById(R.id.et_main_upasss); } public void loginGet(View view) { String uname = et_main_uname.getText().toString(); String upass = et_main_upasss.getText().toString(); String path = getString(R.string.server_name) + "login"; new MyTask().execute(uname, upass, path, "GET"); } public void loginPost(View view){ String uname = et_main_uname.getText().toString(); String upass = et_main_upasss.getText().toString(); String path = getString(R.string.server_name) + "login"; new MyTask().execute(uname, upass, path, "POST"); } public void loginAnyncHttp(View view){ String uname = et_main_uname.getText().toString(); String upass = et_main_upasss.getText().toString(); String path = getString(R.string.server_name) + "login"; AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); RequestParams requestParams = new RequestParams(); requestParams.put("uname",uname); requestParams.put("upass",upass); asyncHttpClient.post(path,requestParams,new TextHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, String responseBody) { super.onSuccess(statusCode, headers, responseBody); Toast.makeText(MainActivity.this, responseBody, Toast.LENGTH_SHORT).show(); } }); } private class MyTask extends AsyncTask { private HttpURLConnection httpURLConnection; @Override protected Object doInBackground(Object[] params) { String uname = (String) params[0]; String upass = (String) params[1]; String path = (String) params[2]; String type = (String) params[3]; try { HttpParams httpParams = new HttpParams(); httpParams.put("uname", uname); httpParams.put("upass", upass); if ("GET".equals(type)) { URL url = new URL(path + "?" + httpParams.toString()); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod(type); } else { URL url = new URL(path); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod(type); byte[] data = httpParams.toString().getBytes(); httpURLConnection.setRequestProperty("Content-Length",data.length+""); httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); httpURLConnection.getOutputStream().write(data); } httpURLConnection.setConnectTimeout(5000); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8")); StringBuffer buffer = new StringBuffer(""); String s = null; while ((s = bufferedReader.readLine()) != null) { buffer.append(s); } return buffer.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { Toast.makeText(MainActivity.this, o.toString(), Toast.LENGTH_SHORT).show(); } }}

用到的工具类

package com.zking.utils;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/2/26. */public class HttpParams { private List<String> list = new ArrayList<>(); public HttpParams put(String key,String value){ list.add(key + "=" + value); return this; } @Override public String toString() { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < list.size(); i++) { if (i != list.size()-1){ buffer.append(list.get(i) + "&"); }else { buffer.append(list.get(i)); } } return buffer.toString(); }}

权限

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

注意如果要使用AsyncHttpClient框架的话 需要在 build.gradle文件中加上

useLibrary 'org.apache.http.legacy'
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表