简体   繁体   English

如何使用HttpURLConnection发出oauth2请求

[英]How to use HttpURLConnection to make oauth2 request

I am trying to make a practise using HttpURLConnection to make a Oauth2 request on Android Studio. 我正在尝试使用HttpURLConnection在Android Studio上进行Oauth2请求。 However, when I tried my code on bluestacks, the app crashed, and I didn't find the error. 但是,当我在bluestacks上尝试代码时,该应用程序崩溃了,并且没有发现错误。 Can anyone help me with this? 谁能帮我这个?

Here is my code: 这是我的代码:

package com.example.administrator.practise;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.auth);
        final TextView textView1 = (TextView)findViewById(R.id.Access);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    URL url = new URL("the url/oauth/token");
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setRequestProperty("Content-type", "application/json");
                    connection.setRequestProperty("Accept", "application/json");
                    connection.setRequestProperty("grant_type", "password");
                    connection.setRequestProperty("username", "email address");
                    connection.setRequestProperty("password", "the password");
                    connection.setRequestProperty("client_id", "7777777");
                    connection.setRequestProperty("client_secret", "RKdZr2tgqS7pdCCR89rcywPJqpFguaZZ9JvbKj6LMWrvuSqK8jBLMq9gqkCQcwet");
                    connection.connect();
                    String response = connection.getResponseMessage();
                    textView1.setText(response);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Try to use AsyncTask in your code. 尝试在代码中使用AsyncTask

Here is my example: 这是我的示例:

public class ThemTask extends AsyncTask<TraSua,Void,Boolean> {
    Activity context;

    public ThemTask(Activity context){
        this.context=context;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        String s="Thêm thất bại";
        if(aBoolean==true){
            s="Thêm thành công";
        }
        Toast.makeText(context,s,Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected Boolean doInBackground(TraSua... params) {

        try{
            TraSua ts=params[0];
            String param="?ban=" + ts.getBan() +
                    "&thucUong=" + ts.getThucUong() +
                    "&doAn=" + ts.getDoAn() +
                    "&them=" + ts.getThem();

            URL url=new URL("http://192.168.0.103/milktea/api/trasua"+param);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("content-type","application/json; charset=utf-8");
            InputStreamReader streamReader=new InputStreamReader(connection.getInputStream(),"UTF-8");
            BufferedReader bufferedReader=new BufferedReader(streamReader);
            StringBuilder builder=new StringBuilder();
            String line=bufferedReader.readLine();
            while(line!=null){
                builder.append(line);
                line=bufferedReader.readLine();
            }
            String result=builder.toString();
            return result.contains("true");
        }catch(Exception e){
            Log.e("Loi_goi",e.toString());
        }
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Scribe通过POST向自定义API发出OAuth2授权请求 - How to make OAuth2 Authorization Request via POST to Custom API using Scribe HttpUrlConnection-Android应用-带有Spring Web Service的oauth2授权 - HttpUrlConnection - android app - oauth2 authorization with spring web service 如何使用 HttpURLConnection 将 GET 请求与 Web API 一起用于 JSON 响应? - How use GET request with web API for JSON response using HttpURLConnection? 如何更改此代码以使用HttpURLConnection代替httpClient - How do I change this code to make use of HttpURLConnection instead of httpClient 如何使HttpURLConnection使用PC的代理而不是通过代码? - How to make HttpURLConnection use the PC's proxy and not via code? 具有OAuth2授权的Android Volley请求 - Android Volley Request with OAuth2 Authorization 如何使Android POST HttpURLConnection - How to make Android POST HttpURLConnection 如何从头开始发出可用于 Oauth 和不同 api 调用的 HTTP 请求的基础知识 - Basics of how to make HTTP request from ground up which can be use for Oauth and diiferent api calls 有没有AccountManager即可轻松使用oAuth2的简便方法吗? - Is there an easy way to use oAuth2 without an AccountManager? Android 与 Kotlin - 如何使用 HttpUrlConnection - Android with Kotlin - How to use HttpUrlConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM