Java 如何在android中将json字符串作为输入参数发送到POST请求
在向POST请求发送JSON字符串时遇到问题 这是我的网址: 关键字:查询 价值:Java 如何在android中将json字符串作为输入参数发送到POST请求,java,Java,在向POST请求发送JSON字符串时遇到问题 这是我的网址: 关键字:查询 价值: { "jql": "project=<projectkey>", "startAt": 0, "maxResults": 100, "fields": [ "summary", "customfield_10006", "status", "description" ] } 试试看{ URL=新URL(“http://172.25.183.
{ "jql": "project=<projectkey>",
"startAt": 0,
"maxResults": 100,
"fields": [
"summary",
"customfield_10006",
"status",
"description"
]
}
试试看{
URL=新URL(“http://172.25.183.183:8080/JIRAservice/rest/runquery");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
字符串projectKey=Home.savedid;
List params=new ArrayList();
添加(新的BasicNameValuePair(“查询”,“jql\”:\“项目=”+projectKey+“\”,\“startAt\”:0,\“maxResults\”:100,\“fields\”:[“summary\”,\“customfield\ U 10006\”,“status\”,“description\”));
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getQuery(params));
writer.flush();
writer.close();
os.close();
连接();
int response=conn.getResponseCode();
字符串输出=“请求URl”+URl;
输出+=System.getProperty(“line.separator”);
输出+=System.getProperty(“line.separator”)+“响应代码”+Response;
BufferedReader br=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
字符串行=”;
StringBuilder respop=新建StringBuilder();
而((line=br.readLine())!=null){
响应追加(行);
}
br.close();
output+=System.getProperty(“line.separator”)+“来自API的IssueList”+respop.toString();
系统输出打印项次(“发行列表”+输出);
字符串respfield=resp(respop.toString());
System.out.println(“响应”+respfield);
}捕获(畸形){
ae.printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
getQuery()方法
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
private String getQuery(列表参数)引发UnsupportedEncodingException
{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
for(NameValuePair对:params)
{
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(pair.getName(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(pair.getValue(),“UTF-8”);
}
返回result.toString();
}
您已经有了哪部分代码?这在很大程度上取决于服务器端的api。。。您可以构建json对象,并执行异步任务来发布数据。。。但是实现这一点的方法实际上取决于您的服务器键和值应该同时发送。我能够建立连接,但几乎无法发送输入参数。
try{
URL url=new URL("http://172.25.183.183:8080/JIRAservice/rest/runquery");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
String projectKey=Home.savedid;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("query"," { \"jql\": \"project=" + projectKey + "\", \"startAt\": 0, \"maxResults\": 100, \"fields\": [\"summary\",\"customfield_10006\", \"status\", \"description\"] }"));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
int respnse=conn.getResponseCode();
String output="Request URl"+url;
output+=System.getProperty("line.separator");
output+=System.getProperty("line.separator")+"Response Code"+respnse;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line= "";
StringBuilder respop=new StringBuilder();
while((line=br.readLine())!=null){
respop.append(line);
}
br.close();
output +=System.getProperty("line.separator")+"IssueList from API"+respop.toString();
System.out.println("IssueList"+ output);
String respfield=resp(respop.toString());
System.out.println(" response"+ respfield);
}catch(MalformedURLException ae){
ae.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}