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

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

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();
                }



        }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值