Android开发-高级网络组件(1)使用Volley登陆-AndroidStudio

来源:互联网 发布:手环源码 编辑:程序博客网 时间:2024/05/16 03:35
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~


其实之前写过一个专栏,来专门介绍Volley,但是那个时候不会PHP,并且侧重Json的学习来进行博客编写。这次,重写一篇关于Volley博客,是继续前2篇基础网络组件来写的,用上一篇的PHP代码,只不过网络请求,使用Volley而不是使用ApacheHttpClient。方便大家对比Volley的好处,并且可以在学习Volley之前了解网络请求的基础知识。

Android开发-基础网络组件(1)使用HttpURLConnection登陆-AndroidStudio

http://blog.csdn.net/iwanghang/article/details/70685334

Android开发-基础网络组件(2)使用ApacheHttpClient登陆-AndroidStudio

http://blog.csdn.net/iwanghang/article/details/70755739


专栏:AndroidVolleyJson - 博客频道 - CSDN.NET

http://blog.csdn.net/column/details/12824.html


老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:




PHP:

<?php// post过来的数据$action_post = isset($_POST['action']) ? $_POST['action'] : '';$username_post = isset($_POST['username']) ? $_POST['username'] : '';$password_post = isset($_POST['password']) ? $_POST['password'] : '';// get过来的数据$action_get = isset($_GET['action']) ? $_GET['action'] : '';$username_get = isset($_GET['username']) ? $_GET['username'] : '';$password_get = isset($_GET['password']) ? $_GET['password'] : '';if($action_post=='login') {    login($username_post, $password_post);    echo " - from post method";} elseif ($action_get=='login'){    login($username_get, $password_get);    echo " - from get method";} else {    $result = array("login"=>"error");    $json = json_encode($result);    echo $json;}/*用户登录*/function login($username, $password) {        $result = null;        if($username=="iwanghang" && $password=="123") {            $result = array("login"=>$username."---".$password."---success");            $json = json_encode($result);        }else{            $result = array("login"=>"账户名或密码操作");            $json = json_encode($result);        }    echo $json;}

Android:

导包:(搜索  com.android.volley)


MainActivity.java:

package com.iwanghang.volleydemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import com.android.volley.AuthFailureError;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import java.util.HashMap;import java.util.Map;public class MainActivity extends AppCompatActivity {    private TextView textView_info;    // 创建请求队列    RequestQueue requestQueue = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView_info = (TextView) findViewById(R.id.textView_info);        requestQueue = Volley.newRequestQueue(this);    }    // 使用Volley  发送Post请求    public void volleySendPostClick(View view) {        // 请求地址        String url = "http://192.168.1.116/login.php";        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {            @Override            public void onResponse(String response) {                // 返回结果                System.out.println(response);                textView_info.setText(response);            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError error) {                // 错误结果                System.out.println(error);                textView_info.setText(error.toString());            }        }){            @Override            protected Map<String, String> getParams() throws AuthFailureError {                // 请求的内容                Map<String,String> params = new HashMap<>();                params.put("action","login");                params.put("username","iwanghang");                params.put("password","123");                return params;            }        };        // 添加到请求队列        requestQueue.add(request);    }}
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    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"    tools:context="com.iwanghang.volleydemo.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!"        android:id="@+id/textView_info" />    <Button        android:onClick="volleySendPostClick"        android:text="使用Volley  发送Post请求"        android:textAllCaps="false"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/buttonVolleySendPost" /></LinearLayout>
网络访问权限:

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







转载请注明出处:http://blog.csdn.net/iwanghang/



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~

1 0
原创粉丝点击