2011-02-11 42 views
17

我有这个JSON字符串。我想将它发布到服务器(即使用POST方法)。这怎么可以在Android中完成?如何将Android中的数据以JSON格式发布到服务器?

JSON字符串:

{ 
    "clientId": "ID:1234-1234", 
    "device": { 
     "userAgent": "myUA", 
     "capabilities": { 
      "sms": true, 
      "data": true, 
      "gps": true, 
      "keyValue": { 
       "Key2": "MyValue2", 
       "Key1": "myvalue1" 
      } 
     }, 
     "screen": { 
      "width": 45, 
      "height": 32 
     }, 
     "keyValue": { 
      "DevcKey2": "myValue2", 
      "DevcKey1": "myValue1" 
     } 
    }, 
    "time": 1294617435368 
} 

我如何能够形成这个JSON数组并把它发送到服务器?

+0

可能的重复[如何发送JSon作为BODY在POST请求到服务器从Android应用程序?](http://stackoverflow.com/questions/3815522/how-do-i-send-json -as-body-in-a-post-request-to-server-from-an-android-applicatio) – Thilo 2011-02-11 06:25:53

+1

in org.json package(http://developer.android.com/reference/org/json/package- summary.html)它应该拥有构建JSON字符串所需的一切。 – xandy 2011-02-11 06:26:52

+0

@Thilo nop,没有关于如何2形式json字符串的东西.... – 2011-02-11 06:32:22

回答

17

我自己做的。

JSONObject returnedJObject= new JSONObject(); 
JSONObject KeyvalspairJObject=new JSONObject(); 
JSONObject devcKeyvalspairJObject=new JSONObject(); 
JSONObject capabilityJObject=new JSONObject(); 
JSONObject ScreenDimensionsJObject =new JSONObject(); 
JSONObject deviceJObject= new JSONObject(); 
try{ 
    KeyvalspairJObject.put("key1","val1"); 
    KeyvalspairJObject.put("key2","val2"); 
    capabilityJObject.put("sms", false); 
    capabilityJObject.put("data", true); 
    capabilityJObject.put("gps", true); 
    capabilityJObject.put("wifi", true); 
    capabilityJObject.put("keyValue", KeyvalspairJObject); 
    ScreenDimensionsJObject.put("width", 45); 
    ScreenDimensionsJObject.put("height", 45); 
    devcKeyvalspairJObject.put("Devckey1","val1"); 
    devcKeyvalspairJObject.put("DEVCkey2","val2"); 
    deviceJObject.put("userAgent", "MYUserAgent"); 
    deviceJObject.put("capabilities", capabilityJObject); 
    deviceJObject.put("screen", ScreenDimensionsJObject); 
    deviceJObject.put("keyValue", devcKeyvalspairJObject); 

    returnedJObject.put("clientId", "ID:1234-1234"); 
    returnedJObject.put("carrier","TMobile"); 
    returnedJObject.put("device",deviceJObject); 
    returnedJObject.put("time",1294617435); 
    returnedJObject.put("msisdn","1234567890"); 
    returnedJObject.put("timezone","GMT"); 
} 
catch(JSONException e) 
{ 
} 

这就是我们如何发送JSON数据到服务器。

public String putDataToServer(String url,JSONObject returnedJObject) throws Throwable 
{ 
    HttpPost request = new HttpPost(url); 
    JSONStringer json = new JSONStringer(); 
    StringBuilder sb=new StringBuilder(); 


    if (returnedJObject!=null) 
    { 
     Iterator<String> itKeys = returnedJObject.keys(); 
     if(itKeys.hasNext()) 
      json.object(); 
     while (itKeys.hasNext()) 
     { 
      String k=itKeys.next(); 
      json.key(k).value(returnedJObject.get(k)); 
      Log.e("keys "+k,"value "+returnedJObject.get(k).toString()); 
     }    
    } 
    json.endObject(); 


    StringEntity entity = new StringEntity(json.toString()); 
         entity.setContentType("application/json;charset=UTF-8"); 
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); 
    request.setHeader("Accept", "application/json"); 
    request.setEntity(entity); 

    HttpResponse response =null; 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 

    HttpConnectionParams.setSoTimeout(httpClient.getParams(), Constants.ANDROID_CONNECTION_TIMEOUT*1000); 
    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),Constants.ANDROID_CONNECTION_TIMEOUT*1000); 
    try{ 
     response = httpClient.execute(request); 
    } 
    catch(SocketException se) 
    { 
     Log.e("SocketException", se+""); 
     throw se; 
    } 
    InputStream in = response.getEntity().getContent(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
    String line = null; 
    while((line = reader.readLine()) != null){ 
     sb.append(line); 
    } 
    return sb.toString(); 
} 
1

看一看这段代码

https://gist.github.com/9457c486af9644cf6b18

retrieveJSONArray(ArrayList的jsonArray,字符串[]键)retrieveJSONString(ArrayList中的JSONObject)

+0

不是我想要的,但是这个wud帮助.... + 1 .... – 2011-02-11 06:58:39

8

如果你有JSON作为字符串,只需使用常规HTTP连接(URL.openConnection())进行POST即可。不需要解析它或类似的东西。

1
JSONObject returnedJObject= new JSONObject(); 

url = "http://49.50.76.75/website/exxx/api.php"; 

// makeSignupRequest(); 

try { 
    returnedJObject.put("email",eemail); 
    returnedJObject.put("password",passwrd); 
    returnedJObject.put("type","customer"); 
    returnedJObject.put("method","register"); 
    returnedJObject.put("username",usr); 

    try { 
     putDataToServer(url, returnedJObject); 

    } 
    catch (Throwable m) { 
     m.printStackTrace(); 
    } 
} 
0

如果这是不是一个“业余爱好”的应用程序之外的任何只有一个或两个服务电话要,我会考虑在寻找一个异步HTTP或休息库。

有几个值得考虑的,包括谷歌乱射:https://developer.android.com/training/volley/index.html

和广场改造:http://square.github.io/retrofit

排球良好的通用异步HTTP库,同时改造上实现REST客户端,专门集中。

这两个库都将处理您在示例中处理的大部分低级细节。

无论您是继续使用DefaultHttpClient代码还是使用Volley或Retrofit,都一定要确保您的网络请求在后台线程上发生,这样您就不会阻塞UI线程。从你的例子中不清楚你是否这样做。

相关问题