我想在 marshmallow android 中发送 Http 请求?
I want to send Http request in marshmallow android?
我是 android 应用程序的初学者。我开发了 android 应用程序,这是我的第一个应用程序。我为 Android 的所有版本开发了 android 应用程序,我的应用程序基于 URL 请求,这意味着我必须为每个操作发送 URL 请求。我遇到了棉花糖 android 版本的问题,因为它不支持 Http Post 方法。
我在 JAVA 中成功开发了发送登录操作请求的程序。相同的代码尝试在 Android 代码中实现,我遇到了问题。
我必须发送类似“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) {}
}
}
}
给所有版本android一些建议,或者任何其他完成此任务的好方法。我试图找到开发此任务的最佳方法。
我也尝试添加 .jre 文件 org.apache.http.legacy,但是 gradle 找不到这个文件。我还添加了互联网权限。
我可以使用 JSON 请求来完成这个任务吗?如果是,那么如何?
请给出一些例子来完成这个任务。
我在这一行出错:int returnCode = client.executeMethod(method);
我又遇到一个错误:Gradle 同步失败:存根!
所以我无法 运行 SDK 并且无法成功构建项目。
-提前致谢。
- 您可以使用HttpURLConnection
要继续使用 Apache HTTP API,您必须首先在 build.gradle 文件中声明以下 compile-time 依赖项:
android {
使用图书馆 'org.apache.http.legacy'
}
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();
}
}
}
我是 android 应用程序的初学者。我开发了 android 应用程序,这是我的第一个应用程序。我为 Android 的所有版本开发了 android 应用程序,我的应用程序基于 URL 请求,这意味着我必须为每个操作发送 URL 请求。我遇到了棉花糖 android 版本的问题,因为它不支持 Http Post 方法。
我在 JAVA 中成功开发了发送登录操作请求的程序。相同的代码尝试在 Android 代码中实现,我遇到了问题。
我必须发送类似“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) {}
}
}
}
给所有版本android一些建议,或者任何其他完成此任务的好方法。我试图找到开发此任务的最佳方法。
我也尝试添加 .jre 文件 org.apache.http.legacy,但是 gradle 找不到这个文件。我还添加了互联网权限。
我可以使用 JSON 请求来完成这个任务吗?如果是,那么如何? 请给出一些例子来完成这个任务。
我在这一行出错:int returnCode = client.executeMethod(method);
我又遇到一个错误:Gradle 同步失败:存根! 所以我无法 运行 SDK 并且无法成功构建项目。
-提前致谢。
- 您可以使用HttpURLConnection
要继续使用 Apache HTTP API,您必须首先在 build.gradle 文件中声明以下 compile-time 依赖项:
android { 使用图书馆 'org.apache.http.legacy' }
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();
}
}
}