Android的HTTP的时候(get和past)
此代码主要包含Http中Get和Post的2种传递方法的使用,别忘了在配置内加入访问INTENET的权限
MainActivity
package David.HTTP;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button get;
private Button post;
private EditText name;
private EditText age;
private String geturi="http://localhost:8080/service/name";
//创建一个HTTP响应对象
private HttpResponse httpResponse;
private HttpEntity httpEntity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
get=(Button)findViewById(R.id.get);
post=(Button)findViewById(R.id.post);
name=(EditText)findViewById(R.id.name);
age=(EditText)findViewById(R.id.age);
post.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String sname=name.getText().toString();
String sage=age.getText().toString();
//通过NameValuePair对象来放入键值对.
NameValuePair nameValuePair=new BasicNameValuePair("name", sname);
NameValuePair nameValuePair2=new BasicNameValuePair("age",sage);
//使用List添加
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(nameValuePair);
list.add(nameValuePair2);
InputStream inputStream=null;
try {
//将List添加到HTTPEntity对象中
HttpEntity entity=new UrlEncodedFormEntity(list);
//添加URL
HttpPost httpPost=new HttpPost(geturi);
//将HttpEntity对象设置到HttpPost对象中传递
httpPost.setEntity(httpEntity);
//生成Http客户端
HttpClient client=new DefaultHttpClient();
//调用execute()方法将请求发送到服务器响应
httpResponse=client.execute(httpPost);
getResult(httpResponse);
}catch (Exception e) {
// TODO: handle exception
}
}
});
get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String sname=name.getText().toString();
String sage=age.getText().toString();
String uri=geturi+"?"+"name="+sname+"&age="+sage;
//生成一个请求对象
HttpGet httpGet=new HttpGet(uri);
//生成一个Http客户对象
HttpClient clien=new DefaultHttpClient();
try {
httpResponse =clien.execute(httpGet);
getResult(httpResponse);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private String getResult(HttpResponse httpResponse){
HttpClient clien=new DefaultHttpClient();
String resuit=null;
InputStream inputStream=null;
try {
//调用getEntity()方法讲响应的内容传入httpEntity
httpEntity=httpResponse.getEntity();
inputStream=httpEntity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=reader.readLine())!=null) {
resuit+=line;
}
System.out.print(resuit);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resuit;
}
}


浙公网安备 33010602011771号