0

ror Webサイトから取得してリストビューに表示するためにjsonを使用してAndroidアプリを作成しました.今、アプリからデータを追加したいと思います. 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<Application> 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<String, Void, String>
{
    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<Application> apps = new ArrayList<Application>();
            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リストビューに表示し、ウェブサイトにも表示したいと考えています。

追加のようなメニューボタンをクリックすると1つのボタンを作成する場合、そのボタンには1つのページが表示され、そのページでデータを追加して保存をクリックする必要があり、リストビューに表示してWebサイトにも投稿する必要があります

どうすればそれができますか。

4

3 に答える 3

0

実行する必要がある主な手順は次のとおりです。

Android アプリにコードを追加して、データをサーバーに送信します。
これを行う方法については、オンラインで多くの例を見つけることができます。一例を次に示します: HTTPClient を使用して JSON で POST 要求を送信する方法は?

送信する JSON データを処理できる Web サービスを用意します。
これを行う方法は、使用しているサーバー側の技術によって異なります。

ListView に関連付けられているリストを更新します

于 2013-05-23T11:58:13.057 に答える