如何将json字符串作为输入参数发送到android
问题描述:
中的POST请求,并在将JSON字符串发送到POST请求时遇到问题。如何将json字符串作为输入参数发送到android
这是我的网址:http://172.25.183.183:8080/JIRAservice/rest/runquery
键:查询
值:
{ "jql": "project=<projectkey>",
"startAt": 0,
"maxResults": 100,
"fields": [
"summary",
"customfield_10006",
"status",
"description"
]
}
哪里<projectkey>
是存储在共享偏好的价值,请帮助
这是我的代码
try{
TextView op=(TextView) findViewById(R.id.resp);
URL url=new URL("http://172.25.183.183:8080/JIRAservice/rest/runquery");
HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
conn.setRequestMethod("POST");
String projectKey=Home.savedid;
JSONObject jsonParam = new JSONObject();
jsonParam.put("query", " "{ \"jql\": \"project=" + projectKey + "\", \"startAt\": 0, \"maxResults\": 100, \"fields\": [\"summary\",\"customfield_10006\", \"status\", \"description\"] }"");
how to send the parameters??
conn.setDoOutput(true);
DataOutputStream dbstrm=new DataOutputStream(conn.getOutputStream());
dbstrm.flush();
dbstrm.close();
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")+respop.toString();
op.setText(output);
}catch(MalformedURLException ae){
ae.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
答
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();
}
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();
}
哪一部分的代码,你已经? – devnull69
这很大程度上取决于服务器端的api ...您可以构建json对象并执行异步任务来发布您的数据......但是执行此操作的方法真的取决于您的服务器 –
关键和值应该同时发送 – Sireesha