2013-10-23 160 views

回答

0

我用下面的代码实现了这一点,并使用JSONParser解析JSON

try { 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost("my_url"); 
    List parameters = new ArrayList(2); 
    JSONObject jsonObject = new JSONObject(); 
    jsonObject.put("par_1", "1"); 
    jsonObject.put("par_2", "2"); 
    jsonObject.put("par_3", "3"); 
    parameters.add(new BasicNameValuePair("action", "par_action")); 
    parameters.add(new BasicNameValuePair("data", jsonObject.toString())); 
    httpPost.setEntity(new UrlEncodedFormEntity(parameters)); 
    HttpResponse httpResponse = httpClient.execute(httpPost); 
    Log.v("Server Application", EntityUtils.toString(httpResponse.getEntity())+" "+jsonObject.toString()); 

} catch (UnsupportedEncodingException e) { 
    Log.e("Server Application", "Error: " + e); 
} catch (ClientProtocolException e) { 
    Log.e("Server Application", "Error: " + e); 
} catch (IOException e) { 
    Log.e("Server Application", "Error: " + e); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
相关问题