您的位置:首页 > 其它

获取用户输入的信息,并用Post请求提交

2015-10-23 22:06 441 查看
[code]package com.example.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.util.WebUtil;

public class PostInfoActivity extends Activity{

    private Button postInfoButton;
    private EditText editTitle;
    private EditText editContent;
    private String inputTitle,inputContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_info);

        postInfoButton = (Button) findViewById(R.id.btn_postInfo);
        editTitle = (EditText) findViewById(R.id.edit_title);
        editContent = (EditText) findViewById(R.id.edit_content);

        postInfoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                new AsyncTask<Void, Void, Void>(){

                    @Override
                    protected Void doInBackground(Void... arg0) {
                        inputTitle = editTitle.getText().toString();
                        inputContent = editContent.getText().toString();
                        /*Toast.makeText(PostInfoActivity.this, inputTitle, Toast.LENGTH_SHORT).show();*/   
                        WebUtil webUtil = new WebUtil();
                        webUtil.postNewsInfo(inputTitle, inputContent);
                        return null;
                    }

                }.execute();
                //跳转至主页...
                Intent intent = new Intent(PostInfoActivity.this,ThirdActivity.class);
                startActivity(intent);
            }
        });
    }

}


WebUtil.java

[code]package com.example.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.example.pojo.NewsItem;;

public class WebUtil {

         //String myURL = "http://10.0.2.2:8080/helloworld/HelloData";
         String getURL = "http://120.25.125.185/helloworld/HelloData";
         String postURL = "http://120.25.125.185/helloworld/PublishData";   
        public  List<NewsItem> getNewsInfo(){
            List<NewsItem> mList = new ArrayList<NewsItem>();

                try {
                    //HttpClient是一个接口,无法创建它的实例,通常情况下都会创建一个DefaultHttpClient的实例
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet =  new HttpGet(getURL);
                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        Log.i("yeye", "get运行了");
                        HttpEntity entity = httpResponse.getEntity();
                        String result = EntityUtils.toString(entity,"utf-8");
                        //JSON字符串解析
                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject nObject = (JSONObject) jsonArray.get(i);
                                NewsItem news = new NewsItem();
                                news.setTitle(nObject.getString("myTitle"));
                                news.setContent(nObject.getString("myContent"));
                                mList.add(news);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return mList;

        }

        public void postNewsInfo(String title,String content){
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(postURL);
            List<NameValuePair> mList = new ArrayList<NameValuePair>();
            mList.add(new BasicNameValuePair("title", title));
            mList.add(new BasicNameValuePair("content", content));

                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(mList,"utf-8"));
                    HttpResponse response = httpClient.execute(httpPost);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        Log.i("yeye", "我开始接受了.."+title+content);
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: