如何在json中转换字符串并在android中传递给webservice
我正在安卓系统中开发web服务,如何在json中转换字符串并在android中传递给webservice,android,json,web-services,Android,Json,Web Services,我正在安卓系统中开发web服务, 我有三根弦 String username = "Rajesh"; String password = "abc" String usertype = "member"; 我想以下面的形式将它们发送到web服务 {"UserName":username,"Password":password,"UserType":usertype} 像 {"UserName":"Rajesh","Password":"abc","UserType":"member"
我有三根弦
String username = "Rajesh";
String password = "abc"
String usertype = "member";
我想以下面的形式将它们发送到web服务
{"UserName":username,"Password":password,"UserType":usertype}
像
{"UserName":"Rajesh","Password":"abc","UserType":"member"}
web服务url类似于:
我不知道怎么做,请帮帮我,如何将字符串转换为上面所述的json,以及如何将其传递给web服务
谢谢你你可以试试这个,
JSONObject.put()
使用一个键值对,所以根据需要执行以下操作:
try {
JSONObject object = new JSONObject();
object.put("username", "rajesh");
object.put("password", "password");
} catch (JSONException e) {
e.printStackTrace();
}
形成所需的JSONObject
后,可以使用HttpClient
将其发送到请求,请参阅
您也可以参考我在android上发出
Http
请求的项目。是的,您可以通过Json对象使用,如下所示
JSONObject jsonObj = new JSONObject();
jsonObj.put("UserName", "Rajesh");
jsonObj.put("Password", "abc");
jsonObj.put("UserType", "member");
如果您发送webservice,那么
String json = jsonObj.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
也检查
}它是一个json对象。它的创建是琐碎的,我不知道它,你能帮我吗?好的,谢谢你,然后我怎样才能把它传递给web服务?你能给我完整的代码吗?请查看编辑后的答案,如果这对你有帮助,请不要忘记接受:)我还与你共享了一个项目@Rajeshsutharplese如果你觉得我的问题对其他人有用,请投票赞成。谢谢你,然后我如何将它传递给web服务?你能给我完整的密码吗?
package com.aven.qqdemo;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUutis {
private void toWebService(){
JSONObject json = new JSONObject();
String username = "Rajesh";
String password = "abc";
String usertype = "member";
putJson(json,"UserName", username);
putJson(json,"Password", password);
putJson(json,"UserType", usertype);
String jsonString = json.toString();
//Send jsonString to web service.
}
private void putJson(JSONObject json, String key, String value){
try {
json.put(key, value);
} catch (JSONException e) {
//Error happened.
}
}