用post方法向服务器请求数据,在这里总结一下。
目的:采用post方法向服务器请求数据
主要有两个activity,MainActivity:显示界面,有一个按钮和一个textview,按钮摁下后采用post方式向服务器请求数据,在textview中显示返回结果。MyHttpClient:向服务器请求的处理方式。
MainActivity:
package com.example.yaoshuju;
import com.example.yaoshuju.MyHttpClient;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String ABC="MainActivity";
TextView back=null;
MyHttpClient hClient=new MyHttpClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button postButton=(Button) findViewById(R.id.button2);
postButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//params.put("test");
String resultString=hClient.DoPost();
back.setText(resultString);
Toast.makeText(MainActivity.this, "Success", 1).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "错误", 1).show();
Log.e(ABC, e.toString());
}
}
});
back=(TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyHttpClient:
package com.example.yaoshuju;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class MyHttpClient {
public static String DoPost() {
String resultString = null;
HttpPost hPost = new HttpPost(
"http://.");
// String url="http://";
// 设置HTTP POST请求参数必须用NameValuePair对象
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new BasicNameValuePair("type", ""));
values.add(new BasicNameValuePair("action", ""));
// NameValuePair nvp1=new BasicNameValuePair("test", "001");
// values.add(nvp1);
HttpEntity entity;
try {
// 设置HTTP POST请求参数
entity = new UrlEncodedFormEntity(values, "UTF-8");
hPost.setEntity(entity);
// Toast.makeText(MainActivity.this, "Success", 1).show();
System.out.println("executing request " + hPost.getURI());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient hc = new DefaultHttpClient();
HttpResponse response;
try {
response = hc.execute(hPost);
HttpEntity entity1 = response.getEntity();
InputStream is = entity1.getContent();
BufferedReader bReader = new BufferedReader(new InputStreamReader(
is));
resultString = bReader.readLine();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultString;
}
}
xml文件:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="post方式" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:text="TextView" />
</RelativeLayout>
这样就能采用post方式向服务器请求数据了。