[Android] study notes using okHttp be landing POST

Why use okHttp?

For someone like me, "unarmed" beginners, just getting started with Android httpUrlConnection, totally unaware of the course, face a long list of codes, was the name of backing out. "Why Andrews network requests such trouble? Is there like a web front end in js framework JQUERY, proprietary network provides a simplified Andrews plead frame it?." The answer is: yes. That is okHttp! okHttp address

Use okHttp be landing POST

okHttp documentation Introduction and clear, are related to the case, read it to understand. As for how to choose, how to use, there are related cases, to conduct POST landing, you have to submit the form data (user names, passwords, etc.) for verification. Then navigate to the use of the following picture:

The method prompts, write the following code:

package com.okhttp.okhttp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import java.io.IOException;

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

public class PostActivity extends AppCompatActivity {

    private final OkHttpClient client=new OkHttpClient();
    private TextView postTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);

        postTextView=(TextView) findViewById(R.id.tvPost);
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

        public void run() throws Exception {
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("username","username")
                    .addFormDataPart("password","password")
                    .addFormDataPart("method","login")
                    .addFormDataPart("key","*******")
                    .build();
            Request request=new Request.Builder()
                    .url("https://zgyu******.php")
                    .post(requestBody)
                    .build();
            Call call=client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    if(response.isSuccessful()){
                        final  String content=response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("TAG",content);
                                postTextView.setText(content);
                            }
                        });
                    }
                }
            });
        }
}

Get the server returns the string, the landing success!

to sum up

okHttp greatly simplifies network Andrews request code, for beginners is very helpful in the future to learn, you can learn more andorid framework, starting with the frame start, simplifies the learning process and reduce the learning curve and improve learning efficiency.

Published 44 original articles · won praise 21 · views 30000 +

Guess you like

Origin blog.csdn.net/gzyh_tech/article/details/81257024