K
K
kiddle2017-10-31 13:32:24
Java
kiddle, 2017-10-31 13:32:24

How to make http request in andoird?

Hello, I'm trying to authorize with a post request in json form. But in logcat it says that it is impossible to make requests in the main thread. How to delimit threads or is there a way to do it in one thread?

package com.example.xiaomi.TrackMove;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
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.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

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

        Button loginButton = (Button) findViewById(R.id.LoginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Login();
            }
        });
    }

    public void Login() {
        EditText loginText = (EditText) findViewById(R.id.LOGIN);
        EditText passText = (EditText) findViewById(R.id.PASS);
        TextView textView = (TextView) findViewById(R.id.OUT);

        textView.setText("WORK!");
        try {
            JSONObject json = new JSONObject();
            json.put("username", loginText.getText().toString());
            json.put("password", passText.getText().toString());
            json.put("apikey", 1);


            URL url = new URL(link);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json");
            con.setUseCaches(false);

            OutputStream out = new BufferedOutputStream(con.getOutputStream());
            out.write(json.toString().getBytes());
            out.flush();
            out.close();

            StringBuilder sb = new StringBuilder();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                textView.setText("WORK!");
                InputStream is = con.getInputStream();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is, "utf-8"));
                String line = "";
                while ((line = br.readLine()) != null)
                    sb.append(line + "\n");
                br.close();
                textView.setText(sb);
                Intent intent = new Intent(this, TasksActivity.class);
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MyOnAsSalat, 2017-11-06
@MyOnAsSalat

In the main thread (ui - responsible for the operation of the interface), the request cannot be made, it will display an error (Google forbade applications not to freeze). Use an asynchronous thread for the request.

M
mr_serg77, 2017-10-31
@mr_serg77

Push it into asynctask.
In general, use OkHttp + Retrofit.

D
Dmtm, 2017-11-02
@Dmtm

asynctask is not designed for requests, somewhere even in the documentation it was mentioned,
because asynctask lives until the first screen rotation,
use Thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question