我想在Android中发送一个简单的POST请求,其主体等于:[{“value”:[“test”]}]
由于请求非常简单,我尝试使用此代码执行此操作:
try {
URL url = new URL("******");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
//headers (all of them are simple strings)
connection.setRequestMethod("POST");
connection.setRequestProperty("X-OAPI-Key", "123****");
connection.setRequestProperty("X-ISS-Key", "*******");
connection.setRequestProperty("Content-Type", "application/json");
//body that I want to send
String urlParameters = "[{\"value\":[test]}]";
// Send post request
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
你能帮我解决这个问题吗?我不知道如何以这种形式准确发送身体?
谢谢!
答案 0 :(得分:0)
创建一个单独的类来解析JSON
,如下所示:
public class JSONParser {
/* Defining all variables */
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
/* Get Json object as respective response to url */
public JSONObject getJSONFromUrl(String url, JSONObject jObj) {
Log.v("Requ. URL: ",url);
// Making HTTP request
try {
// Default Http Client
DefaultHttpClient httpClient = new DefaultHttpClient();
// Http Post Header
HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(jObj.toString());
httpPost.setEntity(se);
httpPost.setHeader("Content-type", "application/json");
// Execute Http Post Request
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
try {
// Getting Server Response
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
// Reading Server Response
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
Log.e("JSON Parser", jObj.toString());
return jObj;
}
}
从AsyncTask
// Async Class for fetching live metal price from webapi
private class YourClassToFetchData extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params) {
jParser = new JSONParser();
JSONObject jsonObject = jParser.getJSONFromUrl(
URL, getConvertedinJson());
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
//创建JSON
对象
private JSONObject getConvertedinJson() {
JSONObject object = new JSONObject();
try {
object.put("", "");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("JObj", "JObj " + object.toString());
return object;
}