Android 向服务器提交数据(GET-POST-AsyncHttpClient)
2017-03-05 10:15
435 查看
登录的demo:
先在清单文件下配置权限
布局文件:
提交数据:
先在清单文件下配置权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
布局文件:
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.android31_commitdatatoserver.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="admin" android:id="@+id/tv_main_name" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="123456" android:id="@+id/tv_main_pass" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录(GET)" android:onClick="loginByGet" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录(POST)" android:onClick="loginByPost" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录(AcyncHttpClient)" android:onClick="loginByAcyncHttpClient" /> </LinearLayout>
提交数据:
public class MainActivity extends AppCompatActivity { private EditText tv_main_name; private EditText tv_main_pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_main_name = (EditText) findViewById(R.id.tv_main_name); tv_main_pass = (EditText) findViewById(R.id.tv_main_pass); } public void loginByGet(View view){ String uname=tv_main_name.getText().toString(); String upwd=tv_main_pass.getText().toString(); String path=getString(R.string.login)+"getlogin.xhtml"; //可变数组 new MyTask().execute(uname, upwd, path, "GET"); Log.i("test",uname+"==get=="+upwd); } public void loginByPost(View view){ String uname=tv_main_name.getText().toString(); String upwd=tv_main_pass.getText().toString(); String path=getString(R.string.login)+"/getlogin.xhtml"; //可变数组 new MyTask().execute(uname, upwd, path, "POST"); Log.i("test",uname+"==post=="+upwd); } public void loginByAcyncHttpClient(View view){ String uname=tv_main_name.getText().toString(); String upwd=tv_main_pass.getText().toString(); String path=getString(R.string.login)+"/getlogin.xhtml"; AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); RequestParams requestParams=new RequestParams(); requestParams.put("uname",uname); requestParams.put("upwd",upwd); // 用post提交 路径 参数 响应 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(); } @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 = 98c3 objects[0].toString(); String upass = objects[1].toString(); String path = objects[2].toString(); String type = objects[3].toString(); String str="uname="+uname+"&upwd="+upwd; 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","application/x-www-form-urlencoded"); //设置允许对外输出数据 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 reault=br.readLine(); return reault; } } 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(MainActivity.this, "==="+s, Toast.LENGTH_SHORT).show(); } } }
相关文章推荐
- 32.Android端向web端(服务器)提交数据(GET-POST-AsyncHttpClient)
- Android之向服务器提交数据(POST,GET,AsyncHttpClient)
- android 向服务器提交数据(get-post-AsyncHttpClient)
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- Android-向服务器提交数据(GET-POST-AsyncHttpClient)
- android之向服务器提交数据(GET,POST,AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- android get或post及HttpClient与服务器数据交互
- Android - GET请求,POST请求,数据提交,编码,HttpClient,async-http开源框架,多线程下载,断点续传,xUtils框架
- Android-使用GET、POST、AsyncHttpClient向服务器提交数据
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- Android HttpClient框架get和post方式提交数据(非原创)
- 与服务器进行数据Android--->servlet(get、post、AsyncClient)三种方式
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据