获取用户输入的信息,并用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(); } } }
相关文章推荐
- Tomcat启动报java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException
- Objective-C-类和对象
- JavaScript之 ------ 一些常用的对象
- ios深度解析之Swift(结构体和类,属性)
- QQ登录界面01
- 整理分类Android文件(一)
- 什么是指针?如何理解指针?指针又是如何定义与使用的?
- 函数式编程--函数式接口
- iOS学习笔记网络篇之文件上传(复杂,详解----)
- 函数式编程--函数式接口
- Animation from External Sources(来自外部的动画)
- 10月23
- 掌趣2016校招大题
- 干货:android实现hessian通信
- ubuntu apt-get update失败 解决方法
- 【web前端】获取url中的参数
- 必修2?!!!!!!!!!!!!
- Basic Calculator
- Java反射与线程的入门课
- 抛出异常