I need to pass some parameters to server that i need to pass as below format
{
"k2": {
"mk1": "mv1",
"mk2": [
"lv1",
"lv2"
]
}
}
如何在机器人中生成这种格式。
提前感谢
I need to pass some parameters to server that i need to pass as below format
{
"k2": {
"mk1": "mv1",
"mk2": [
"lv1",
"lv2"
]
}
}
如何在机器人中生成这种格式。
提前感谢
这并不是说,你想要的输出是 JsonObject 内部的JsonArray, JsonObject 内部的JsonObject 和 JsonObject 内部的JsonObbject 。 因此,你可以以静态的方式创建它们,然后可以组合在一起。 如下面一样。
try {
JSONObject parent = new JSONObject();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonArray.put("lv1");
jsonArray.put("lv2");
jsonObject.put("mk1", "mv1");
jsonObject.put("mk2", jsonArray);
parent.put("k2", jsonObject);
Log.d("output", parent.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
<强 > 产出 强 >
{
"k2": {
"mk1": "mv1",
"mk2": [
"lv1",
"lv2"
]
}
}
您可以使用 JSONObject
并用它构建数据。
这里是""http:// developmenter.android.com/reference/org/json/Json/JSONObject.html"
jsonObject.toString() // Produces json formatted object
首先,您必须创建不同的类 HttpUtil.java. 。 见代码
public class HttpUtil {
// lat=50.2911 lon=8.9842
private final static String TAG = "DealApplication:HttpUtil";
public static String get(String url) throws ClientProtocolException,
IOException {
Log.d(TAG, "HTTP POST " + url);
HttpGet post = new HttpGet(url);
HttpResponse response = executeMethod(post);
return getResponseAsString(response);
}
public static String post(String url, HashMap<String, String> httpParameters)
throws ClientProtocolException, IOException {
Log.d(TAG, "HTTP POST " + url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
httpParameters.size());
Set<String> httpParameterKeys = httpParameters.keySet();
for (String httpParameterKey : httpParameterKeys) {
nameValuePairs.add(new BasicNameValuePair(httpParameterKey,
httpParameters.get(httpParameterKey)));
}
HttpPost method = new HttpPost(url);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
System.out.println("**************Request=>"+urlEncodedFormEntity.toString());
method.setEntity(urlEncodedFormEntity);
HttpResponse response = executeMethod(method);
return getResponseAsString(response);
}
private static HttpResponse executeMethod(HttpRequestBase method)
throws ClientProtocolException, IOException {
HttpResponse response = null;
HttpClient client = new DefaultHttpClient();
response = client.execute(method);
Log.d(TAG, "executeMethod=" + response.getStatusLine());
return response;
}
private static String getResponseAsString(HttpResponse response)
throws IllegalStateException, IOException {
String content = null;
InputStream stream = null;
try {
if (response != null) {
stream = response.getEntity().getContent();
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur + "
");
}
content = sb.toString();
System.out.println("**************Response =>"+content);
}
} finally {
if (stream != null) {
stream.close();
}
}
return content;
}
}
示例是帮助您调用此函数, 它会将 JSON 返回为字符串值。 尝试它
public String getResult() {
JSONObject userResults = null;
try {
userResults = new JSONObject();
userResults.put("valueOne",str_one);
userResults.put("valueTwo", str_two);
userResults.put("valueThree" ,str_three);
userResults.put("valueFour", str_four);
} catch (Exception e) {
e.printStackTrace();
}
return userResults.toString();
}
I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...
I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...
I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...
I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...
To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?
When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...
I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...
I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...