如何改变一个HttpsURLConnection GET是一个POST请求?



我对android(一般编程)真的很陌生,但我继承了一个由另一个人创建的项目,我知道这对你们很多人来说可能很简单,但我失去了试图改变下面的代码。

我需要做的是将请求的类型从GET更改为POST,并随请求发送一些值。

请求需要有以下语法:

type=active
data={"json here with all info"} ------> mRequestStringEncoded

String RequestString = ((myrequest) request).getJson();
String mRequestStringEncoded = URLEncoder.encode( RequestString, "utf-8" );
mURL = defautlUrl+ mRequestStringEncoded;
Log.e( TAG, "Request URL: " + mURL );

try
{
HttpsURLConnection mUrlConnection = (HttpsURLConnection) new URL( mURL ).openConnection();
mUrlConnection.setRequestProperty( "charset", "utf-8" );
mUrlConnection.setRequestMethod( "GET" ); 
mUrlConnection.setConnectTimeout( 12000 );
mUrlConnection.setReadTimeout( 30000 );
mUrlConnection.connect();

我知道我需要改变:

mUrlConnection.setRequestProperty( "charset", "utf-8" );
mUrlConnection.setRequestMethod( "GET" ); 

:

mUrlConnection.setRequestProperty("Content-Type", "application/json; utf-8");
mUrlConnection.setRequestMethod( "POST" );

但是如何传递参数呢?

试试这样:

String post_data="type=active&data=" + data;
HttpsURLConnection mUrlConnection = (HttpsURLConnection) new URL( tURL ).openConnection();
mUrlConnection.setRequestMethod( "POST" );
mUrlConnection.setRequestProperty("type", "active");
mUrlConnection.setRequestProperty("data", "data"); 
mUrlConnection.setDoOutput(true);
//Adding Post Data
OutputStream outputStream = mUrlConnection.getOutputStream();
outputStream.write(post_data.getBytes());
outputStream.flush();
outputStream.close();

mUrlConnection.setConnectTimeout( 22000 );
mUrlConnection.setReadTimeout( 30000 );
mUrlConnection.connect();

你差点就成功了。试试这个,应该管用。我使用Jackson从Map获取json格式:

package com.http;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class FormSubmitService {
public void doSubmit(String url, Map<String, String> data) throws IOException {
URL siteUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setUseCaches (true);
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String content = getJsonFromMap(data);
System.out.println(content);
out.writeBytes(content);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line=in.readLine())!=null) {
System.out.println(line);
}
in.close();
}

private String getJsonFromMap(Map<String, String> map) {
ObjectMapper objectMapper = new ObjectMapper();
String json = null;
try {
json = objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}

}

在POST方法中,您可以通过使用Gson将参数转换为JSON来传递参数。

步骤1:在Gradle文件中添加gson依赖项

dependencies {
implementation 'com.google.code.gson:gson:2.8.8'
}

步骤2:创建一个参数/键的模型类。

public class ApiModel {
public String type,data;
public ApiModel(String type, String data) {
this.type = type;
this.data = data;
}
public String getType() {
return type;
}
public String getData() {
return data;
}
}

步骤3:创建模型类的对象并添加值。

//add data into model to create json
ApiModel ObjApi = new ApiModel(type_value, data_value);

步骤4:现在,转换ObjApi

Gson gson = new Gson();
String json = gson.toJson(ObjApi);

第五步:添加json字符串到BufferedWriter.

OutputStream stream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(stream, "UTF-8"));
bufferedWriter.write(json);
bufferedWriter.flush();
bufferedWriter.close();
stream.close();

例子:使用httpurlConnection.

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(REQUEST_METHOD);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setConnectTimeout(TimeOut);
httpURLConnection.setReadTimeout(TimeOut);
OutputStream stream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(stream, "UTF-8"));
bufferedWriter.write(json);
bufferedWriter.flush();
bufferedWriter.close();
stream.close();
httpURLConnection.disconnect();

最新更新