【问题标题】:Send data to server through JSON通过 JSON 向服务器发送数据
【发布时间】:2013-10-20 18:30:25
【问题描述】:

我是 Android 程序员的新手,我想通过 JSON 以以下格式将数据发送到服务器,并以这种格式实现 Json..并且还想从服务器获取数据..

{

    "signup": [

    {
    "username": "test1264",
    "password": "1234",
    "email": "example@gmail.com",
    "phoneno": "223344556",
    "altphoneno": "12345678",
    "firstname": "abc",
    "lastname": "xyz"
    }    ]
}

回复:

成功时:{"status":1}
失败时:{"status":0}

  • 用于用户登录的 JSON:
{"login":[
      {"username":"test1234",
       "password":"1234"}
       ]}
Response:
on success:  {
"user": [
{
"firstname": "abc",
"lastname": "xyz",
"email": "example@gmail.com",
"phone": "99887766",
"username": "test1234"
}
]
}

失败时:{"error":["Auth error"]}

【问题讨论】:

    标签: android json


    【解决方案1】:

    你可以使用这样的代码

    // 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);
    

    不要忘记将此添加到非 UI 线程。

    【讨论】:

      【解决方案2】:

      您可以使用droidQuery 非常轻松地做到这一点:

      $.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 服务。

      【讨论】:

        【解决方案3】:
        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();
        

        【讨论】:

          猜你喜欢
          • 2014-11-20
          • 1970-01-01
          • 2013-11-17
          • 1970-01-01
          • 2019-01-01
          • 2017-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多