I want to send Http request in marshmallow android

2019-09-10 06:39发布

I am beginner for android application. I develop android application and its my first application. I develop android application for all version of Android, my application based on URL request, means I have to send URL request for every action. I faced problem for marshmallow android version, because It isn't support Http Post method.

I developed program successfully in JAVA to send request for login action. Same code try to implement in Android code, I faced problem.

I have to send request like "https://www.veejansh.com/vurja/mobile.php?action=login&username=abc@xyz.com&password=blabla".

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.omg.CORBA.NameValuePair;

/**
 *
 * @author tejaskumar
 */
public class tryHttp2 {
    public static void main (String args[]){
        HttpClient client = new HttpClient();
        client.getParams().setParameter("https.useragent", "Test Client");

        BufferedReader br = null;

        PostMethod method = new PostMethod("https://www.veejansh.com/vurja/mobile.php");
        method.addParameter("action", "login");
        method.addParameter("username","abc@xyz.com");
        method.addParameter("password","blabla");

        try{
            int returnCode = client.executeMethod(method);
            if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                System.err.println("The Post method is not implemented by this URI");
                // still consume the response body
                method.getResponseBodyAsString();
            } else {
                br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
                String readLine;
                while(((readLine = br.readLine()) != null)) {
                    System.out.println(readLine);
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        } finally {
            method.releaseConnection();
            if(br != null) try { br.close(); } catch (Exception e) {}
        }
    }
} 

Give some advice for all version of android, or any other good method to achieve this task. I try to find best way to develop this task.

I also try to add .jre file org.apache.http.legacy, but gradle cannot find this file. I also add permission for INTERNET.

Can I achieve this task with use of JSON request? If yes, then how? Please give some example to achieve this task.

I got error on this line : int returnCode = client.executeMethod(method);

I got one more error : Gradle sync failed: Stub! So I cannot run SDK and cannot build project successfully.

-Thanks in advance.

2条回答
甜甜的少女心
2楼-- · 2019-09-10 06:56
  1. You can use HttpURLConnection
  2. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

    android { useLibrary 'org.apache.http.legacy' }

查看更多
乱世女痞
3楼-- · 2019-09-10 06:57
public static Boolean excutePost(String targetURL, JSONObject jsonParam)
    {
        URL url;
        HttpURLConnection connection = null;
        try {
            url = new URL(targetURL);
            connection = (HttpURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST"); // hear you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
            connection.connect();

            //Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream ());
            wr.writeBytes(jsonParam.toString());
            wr.flush();
            wr.close ();

            InputStream is;
            int response = connection.getResponseCode();
            if (response >= 200 && response <=399){
                //return is = connection.getInputStream();
                return true;
            } else {
                //return is = connection.getErrorStream();
               return false;
            }


        } catch (Exception e) {

            e.printStackTrace();
            return false;

        } finally {

            if(connection != null) {
                connection.disconnect();
            }
        }
    }
查看更多
登录 后发表回答