Android 通过JSON向服务器发送数据
我是Android程序员的新手,我想用以下格式通过JSON向服务器发送数据,并用这种格式实现JSON。我还想从服务器获取数据Android 通过JSON向服务器发送数据,android,json,Android,Json,我是Android程序员的新手,我想用以下格式通过JSON向服务器发送数据,并用这种格式实现JSON。我还想从服务器获取数据 网址: 报名 答复: 成功时:{“状态”:1} 失败时:{“状态”:0} 用于用户登录的JSON: 失败时:{“error”:[“Auth error”]}您可以使用如下代码 // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClien
- 网址:
- 报名
{“状态”:1}
失败时:
{“状态”:0}
- 用于用户登录的JSON:
失败时:
{“error”:[“Auth error”]}
您可以使用如下代码
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://fort.example.com");
JSONObject json = new JSONObject();
try {
// Add your data
json.put("key", "value");
StringEntity se = new StringEntity( json.toString());
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String jsonString = reader.readLine();
JSONTokener tokener = new JSONTokener(jsonString);
JSONObject finalResult = new JSONObject(tokener);
别忘了将其添加到NoUI线程。您可以使用以下方法轻松完成此操作:
$.ajax(new AjaxOptions().url("http://fort.example.com")
.type("POST")
.data("\"signup\": [{" +
"\"username\": \"test1264\"," +
"\"password\": \"1234\"," +
"\"email\": \"example@gmail.com\"," +
"\"phoneno\": \"223344556\"," +
"\"altphoneno\": \"12345678\"," +
"\"firstname\": \"abc\"," +
"\"lastname\": \"xyz\"" +
"\"}]")
.dataType("json")
.success(new Function() {
@Override
public void invoke($ d, Object... args) {
JSONObject json = (JSONObject) args[0];
boolean success = json.getBoolean("status");
if (success) {
//handle success
}
else {
//handle error
}
}
})
.error(new Function() {
@Override
public void invoke($ d, Object... args) {
AjaxError error = (AjaxError) args[0];
Log.i("Ajax", "Error " + error.status + ": " + error.reason);
}
}));
您可以在其他情况下重复此逻辑,例如登录web服务
PostMethod method = new PostMethod();
org.apache.commons.httpclient.URI newUri = new org.apache.commons.httpclient.URI(URIstring, true);
method.setURI(newUri);
method.setRequestBody(JsonObj().toString());
method.setRequestHeader("Content-Type", "application/json");
org.apache.commons.httpclient.HttpClient newhttpClient = new org.apache.commons.httpclient.HttpClient();
int statusCode = newhttpClient.executeMethod(method);
return method.getResponseBodyAsString();
$.ajax(new AjaxOptions().url("http://fort.example.com")
.type("POST")
.data("\"signup\": [{" +
"\"username\": \"test1264\"," +
"\"password\": \"1234\"," +
"\"email\": \"example@gmail.com\"," +
"\"phoneno\": \"223344556\"," +
"\"altphoneno\": \"12345678\"," +
"\"firstname\": \"abc\"," +
"\"lastname\": \"xyz\"" +
"\"}]")
.dataType("json")
.success(new Function() {
@Override
public void invoke($ d, Object... args) {
JSONObject json = (JSONObject) args[0];
boolean success = json.getBoolean("status");
if (success) {
//handle success
}
else {
//handle error
}
}
})
.error(new Function() {
@Override
public void invoke($ d, Object... args) {
AjaxError error = (AjaxError) args[0];
Log.i("Ajax", "Error " + error.status + ": " + error.reason);
}
}));
PostMethod method = new PostMethod();
org.apache.commons.httpclient.URI newUri = new org.apache.commons.httpclient.URI(URIstring, true);
method.setURI(newUri);
method.setRequestBody(JsonObj().toString());
method.setRequestHeader("Content-Type", "application/json");
org.apache.commons.httpclient.HttpClient newhttpClient = new org.apache.commons.httpclient.HttpClient();
int statusCode = newhttpClient.executeMethod(method);
return method.getResponseBodyAsString();