如何在android中创建JSON格式的数据?
我需要向服务器传递一些参数,如下所示 格式 那么如何在android中生成这种格式呢 我尝试使用,但它在如何在android中创建JSON格式的数据?,android,json,Android,Json,我需要向服务器传递一些参数,如下所示 格式 那么如何在android中生成这种格式呢 我尝试使用,但它在obj.writeJSONString(out)处显示错误这行。谁能帮我解决这个问题 提前感谢您可以使用JSONObject并用它构建数据 这是你的电话号码 但这并不是说,您想要的输出是JSONObject中的JSONArray和另一个JSONObject中的JSONObject。因此,您可以分别创建它们,然后将它们放在一起。如下 try { JSONObject pa
obj.writeJSONString(out)处显示错误代码>这行。谁能帮我解决这个问题
提前感谢您可以使用JSONObject
并用它构建数据
这是你的电话号码
但这并不是说,您想要的输出是JSONObject中的JSONArray和另一个JSONObject中的JSONObject。因此,您可以分别创建它们,然后将它们放在一起。如下
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"
]
}
}
首先,您必须创建单独的类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 + "\n");
}
content = sb.toString();
System.out.println("**************Response =>"+content);
}
} finally {
if (stream != null) {
stream.close();
}
}
return content;
}
}
公共类HttpUtil{
//纬度=50.2911经度=8.9842
私有最终静态字符串TAG=“DealApplication:HttpUtil”;
公共静态字符串get(字符串url)抛出ClientProtocolException,
IOException{
Log.d(标记“HTTP POST”+url);
HttpGet post=新的HttpGet(url);
HttpResponse响应=执行方法(post);
返回getResponseAsString(响应);
}
公共静态字符串post(字符串url、HashMap httpParameters)
抛出ClientProtocolException,IOException{
Log.d(标记“HTTP POST”+url);
List name valuepairs=new ArrayList(
httpParameters.size());
设置httpParameterKeys=httpParameters.keySet();
用于(字符串httpParameterKey:httpParameterKeys){
添加(新的BasicNameValuePair(httpParameterKey,
get(httpParameterKey));
}
HttpPost方法=新的HttpPost(url);
UrlEncodedFormEntity UrlEncodedFormEntity=新的UrlEncodedFormEntity(nameValuePairs);
System.out.println(“************请求=>”+urlEncodedFormEntity.toString());
方法.setEntity(urlEncodedFormEntity);
HttpResponse响应=执行方法(方法);
返回getResponseAsString(响应);
}
私有静态HttpResponse executeMethod(HttpRequestBase方法)
抛出ClientProtocolException,IOException{
HttpResponse响应=null;
HttpClient=new DefaultHttpClient();
响应=client.execute(方法);
Log.d(标记,“executeMethod=“+response.getStatusLine());
返回响应;
}
私有静态字符串getResponseAsString(HttpResponse响应)
抛出IllegalStateException,IOException{
字符串内容=null;
InputStream=null;
试一试{
if(响应!=null){
stream=response.getEntity().getContent();
InputStreamReader reader=新的InputStreamReader(流);
BufferedReader buffer=新的BufferedReader(读卡器);
StringBuilder sb=新的StringBuilder();
串电流;
而((cur=buffer.readLine())!=null){
sb.追加(cur+“\n”);
}
content=sb.toString();
System.out.println(“************响应=>”+内容);
}
}最后{
if(流!=null){
stream.close();
}
}
返回内容;
}
}
此示例有助于您调用此函数,它将以字符串值的形式返回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();
}
您可以发布异常详细信息吗?您是否正在导入org.json.JSONObject
?+1我对json解析不熟悉。这个清晰的例子就是我所需要的:-)@LalitPoptaniLog.d(“output”,parent.toString(2))中的2
是什么代码>?是间距吗?@Compaqle202x是的,是Json输出中的间距和格式。
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 + "\n");
}
content = sb.toString();
System.out.println("**************Response =>"+content);
}
} finally {
if (stream != null) {
stream.close();
}
}
return content;
}
}
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();
}