前边有一篇使用get和post方式请求站点的文章,此篇使用异步方式请求:
同时解决了android 4.0以后的主线程不能访问网络的问题。
简写了代码和记录以防忘记备查:
自己编写了GetOrPost类继承了AsyncTask 全部代码如下:
package ytbk.soft.getorpost;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.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.os.AsyncTask;
public class GetOrPost extends AsyncTask<String, Integer,String>
{
//要请求的网站地址
private String baseUrl="http://192.168.10.86:8080/service/indexService";
//客户端
private HttpClient client=null;
//请求参数或者返回实体
private HttpEntity entity=null;
//post请求
private HttpPost post=null;
//get请求
private HttpGet get=null;
//返回值
private HttpResponse response=null;
//流对象
private InputStream stream=null;
private String getResult()
{
try {
BufferedReader reder=new BufferedReader(new InputStreamReader(stream));
String result="";
String line="";
while((line=reder.readLine())!=null)
{
result=result+line;
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected String doInBackground(String... params)
{
String name=params[0];
String age=params[1];
try
{
if (params[2].toString().trim().equals("get"))
{
String Url=baseUrl+"?name="+name+"&age="+age;
get=new HttpGet(Url);
client=new DefaultHttpClient();
response=client.execute(get);
}
if (params[2].toString().trim().equals("post"))
{
NameValuePair namevalue=new BasicNameValuePair("name1",name);
NameValuePair agevalue=new BasicNameValuePair("age1",age);
List<NameValuePair> list=new ArrayList<NameValuePair>();
list.add(namevalue);
list.add(agevalue);
HttpEntity entity=new UrlEncodedFormEntity(list);
HttpPost post=new HttpPost(baseUrl);
post.setEntity(entity);
HttpClient client=new DefaultHttpClient();
response=client.execute(post);
}
entity=response.getEntity();
stream=entity.getContent();
return getResult();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
//打印请求数据
System.out.println(result);
super.onPostExecute(result);
}
}
package ytbk.soft.activity;
import ytbk.soft.getorpost.GetOrPost;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
private Button getButton = null;
private Button postButton=null;
private EditText name=null;
private EditText age=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
getButton=(Button)this.findViewById(R.id.getButton);
postButton=(Button)this.findViewById(R.id.postButton);
name=(EditText)this.findViewById(R.id.name);
age=(EditText)this.findViewById(R.id.age);
getButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String sname=name.getText().toString();
String sage=age.getText().toString();
GetOrPost post=new GetOrPost();
post.execute(sname,sage,"get");
}
});
postButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String sname=name.getText().toString();
String sage=age.getText().toString();
GetOrPost post=new GetOrPost();
post.execute(sname,sage,"post");
}
});
}
@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;
}
}