阅读背景:

android客户端与javaweb服务器端数据通信-Post-键值对

来源:互联网 

android studio端

package com.example.z_t.mytest3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class FirstActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private String uname;
    private String pwd;
    private String result;
    private boolean res;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);

        Button button = (Button)findViewById(R.id.login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uname = username.getText().toString();
                pwd = password.getText().toString();
                Log.i("First", uname);
                Log.i("First", pwd);

//                getHttp(uname,pwd, new Callback() {
//                    @Override
//                    public void onFailure(Call call, IOException e) {
//
//                    }
//
//                    @Override
//                    public void onResponse(Call call, Response response) throws IOException {
//                        String responseData = response.body().string();
//                        int ii =responseData.length();
//                        Log.i("First", responseData);
//                        Log.i("First", String.valueOf(ii));
//                        if (responseData.equals("true")){
//                            res=true;
//                        }else {
//                            res=false;
//                        }
//                        Log.i("First", String.valueOf(res));
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                if (res){
//                                    Toast.makeText(FirstActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
//                                    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
//                                    startActivity(intent);
//                                }else {
//                                    Toast.makeText(FirstActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        });
//                    }
//                });
                postHttp(uname, pwd, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String responseData = response.body().string();
                        int ii =responseData.length();
                        Log.i("First", responseData);
                        Log.i("First", String.valueOf(ii));
                        if (responseData.equals("true")){
                            res=true;
                    }else {
                        res=false;
                    }
                        Log.i("First", String.valueOf(res));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (res){
                                    Toast.makeText(FirstActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                                    startActivity(intent);
                                }else {
                                    Toast.makeText(FirstActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                    }
                });



            }
        });
    }

    private void getHttp(String username,String password,okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();

        String url = "https://192.168.150.1:8080/test2/login?name="+username+"&pwd="+password;
        Log.i("First", url);
        Request request = new Request.Builder()
                .url(url)
                .method("GET",null)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);


    }

    private String parseXMLWithPull(String xmlData) {
        try{
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(new StringReader(xmlData));
            int eventType = xmlPullParser.getEventType();
            while (eventType!=XmlPullParser.END_DOCUMENT){
                String nodeName = xmlPullParser.getName();
                switch (eventType){
                    case XmlPullParser.START_TAG:{
                        if ("td".equals(nodeName)){
                            result = xmlPullParser.nextText();
                        }
                        break;
                    }
                    case XmlPullParser.END_TAG:{
                        if ("tr".equals(nodeName)){
                            Log.i("First",result);
                        }
                    }
                    default:break;
                }
                eventType = xmlPullParser.next();
            }


                }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


        private void postHttp(String username,String password,okhttp3.Callback callback){
            OkHttpClient client = new OkHttpClient();
            RequestBody body = new FormBody.Builder()
                    .add("username",username)
                    .add("password",password)
                    .build();
            Request request = new Request.Builder()
                    .url("https://192.168.150.1:8080/test2/login")
                    .post(body)
//                    .addHeader("content-type","multipart/form-data")
                    .build();
            Call call = client.newCall(request);
            call.enqueue(callback);
        }


}

package com.example.z_t.mytes



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: