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.
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' }