在Android中使用Body发送POST请求 [英] send POST requests with Body in Android
本文介绍了在Android中使用Body发送POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Android中发送一个简单的POST请求,其主体等于:{{value:[test]}]
I want to send a simple POST request in Android with a body equaling this : [{ "value": ["test"]}]
自请求非常简单,我尝试使用此代码:
Since the request is very very simple, I tried to do it using this code :
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();
}
你能帮我解决这个问题吗?我不知道如何以这种形式准确发送信息?
Can you help me to fix this problem ? I don't know how can I exactly send a body in this form ?
谢谢!
推荐答案
创建一个单独的类来解析 JSON
,如下所示:
Create a separate class to parse JSON
like this:
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;
}
这篇关于在Android中使用Body发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文