I
I
Igor2020-03-06 12:17:28
Layout of letters
Igor, 2020-03-06 12:17:28

How to send a post request using com.github.kevinsawicki.http.HttpRequest?

I'm trying to send a POST request, I wrote a function:
sSite - site url in the format " https://XXXXX.ru "; Q - request
While for an example imitation of input of login and the password.

public int Post(String Q, String va11, String Val2) {
        Map<String, String> data = new HashMap<String, String>();
        data.put("username", va11);
        data.put("password", Val2);

        HttpRequest request = HttpRequest.post(sSite + Q);
        request.trustAllCerts();
        request.trustAllHosts();

        return request.form(data).code();
    }

As a result, I get an exception without an error description, but Debug has the following information:
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mytestapplication, PID: 25210
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7125) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
        at java.net.InetAddress.getAllByName(InetAddress.java:1152)
        at com.android.okhttp.Dns$1.lookup(Dns.java:41)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:219)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:30)
        at com.github.kevinsawicki.http.HttpRequest.openOutput(HttpRequest.java:2728)
        at com.github.kevinsawicki.http.HttpRequest.send(HttpRequest.java:3047)
        at com.example.mytestapplication.Request.Post(Request.java:17)
        at com.example.mytestapplication.Request.Login(Request.java:13)
        at com.example.mytestapplication.MLConnection.Login(MLConnection.java:9)
        at com.example.mytestapplication.MainActivity.btnLog_Click(MainActivity.java:26)

How to overcome the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2019-07-24
@FitTech

Mailchimp, both in terms of layout and for mailing, is a very good option.

I
illuzor, 2020-03-06
@pingvin156

How to overcome the problem?

Don't access network on ui thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question