如何在使用json的android中使用post方法

我使用json创建了一个Android应用程序,用于从ror网站获取并在listview中显示,现在我想从我们的应用程序添加数据,它必须在我们的应用程序中显示在列表视图中,然后它也必须在网站中显示。如何在我们的应用程序中使用post方法和显示。

得到我喜欢的方法

public class MainActivity extends ListActivity implements FetchDataListener { private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_list_item); initView(); } private void initView() { // show progress dialog dialog = ProgressDialog.show(this, "", "Loading..."); String url = "http://floating-wildwood-1154.herokuapp.com/posts.json"; FetchDataTask task = new FetchDataTask(this); task.execute(url); } @Override public void onFetchComplete(List data) { // dismiss the progress dialog if ( dialog != null ) dialog.dismiss(); // create new adapter ApplicationAdapter adapter = new ApplicationAdapter(this, data); // set the adapter to list setListAdapter(adapter); } @Override public void onFetchFailure(String msg) { // dismiss the progress dialog if ( dialog != null ) dialog.dismiss(); // show failure message Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } } 

fetchdatatask.java

 public class FetchDataTask extends AsyncTask { private final FetchDataListener listener; private String msg; public FetchDataTask(FetchDataListener listener) { this.listener = listener; } @Override protected String doInBackground(String... params) { if ( params == null ) return null; // get url from params String url = params[0]; try { // create http connection HttpClient client = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); // connect HttpResponse response = client.execute(httpget); // get response HttpEntity entity = response.getEntity(); if ( entity == null ) { msg = "No response from server"; return null; } // get response content and convert it to json string InputStream is = entity.getContent(); return streamToString(is); } catch ( IOException e ) { msg = "No Network Connection"; } return null; } @Override protected void onPostExecute(String sJson) { if ( sJson == null ) { if ( listener != null ) listener.onFetchFailure(msg); return; } try { // convert json string to json object JSONObject jsonObject = new JSONObject(sJson); JSONArray aJson = jsonObject.getJSONArray("post"); // create apps list List apps = new ArrayList(); for ( int i = 0; i < aJson.length(); i++ ) { JSONObject json = aJson.getJSONObject(i); Application app = new Application(); app.setContent(json.getString("content")); // add the app to apps list apps.add(app); } //notify the activity that fetch data has been complete if ( listener != null ) listener.onFetchComplete(apps); } catch ( JSONException e ) { e.printStackTrace(); msg = "Invalid response"; if ( listener != null ) listener.onFetchFailure(msg); return; } } /** * This function will convert response stream into json string * * @param is * respons string * @return json string * @throws IOException */ public String streamToString(final InputStream is) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ( (line = reader.readLine()) != null ) { sb.append(line + "\n"); } } catch ( IOException e ) { throw e; } finally { try { is.close(); } catch ( IOException e ) { throw e; } } return sb.toString(); } } 

像这样我使用get方法并显示,为了同样的目的,我想将post方法添加到android listview中显示并在网站中显示。

如果我点击菜单按钮就像创建一个按钮一样,在那一个它会显示一个页面,在那个页面我必须添加数据并点击保存,它必须在列表视图中显示并在网站上发布

我怎么能做那个。

你可以使用HttpPost

  @Override protected String doInBackground(String... params) { if ( params == null ) return null; // get url from params String url = params[0]; try { // create http connection HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(url); try{ StringEntity s = new StringEntity(json.toString()); //json is ur json object s.setContentEncoding("UTF-8"); s.setContentType("application/json"); request.setEntity(s); request.addHeader("Accept", "text/plain"); //give here your post method return type HttpResponse response = client.execute(request); // get response HttpEntity entity = response.getEntity(); if ( entity == null ) { msg = "No response from server"; return null; } // get response content and convert it to json string InputStream is = entity.getContent(); return streamToString(is); 

您必须采取的主要步骤是:

在Android应用中添加代码以将数据发布到您的服务器。
您可以在线找到很多关于如何进行此操作的示例。 这是一个例子: 如何使用HTTPClient在JSON中发送POST请求?

拥有一个可以保存您发送的JSON数据的Web服务。
如何做到这一点取决于您使用的服务器端技术。

更新与ListView关联的列表

  public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://abcd.wxyz.com/"); try { List  nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("IDToken1", "username")); nameValuePairs.add(new BasicNameValuePair("IDToken2", "password")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); if(response != null) { int statuscode = response.getStatusLine().getStatusCode(); if(statuscode==HttpStatus.SC_OK) { String strResponse = EntityUtils.toString(response.getEntity()); } } } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } }