比较通用的传输方法
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button sendRequest;
private TextView responseText;
//=====================================================================================================================
//dialog不能在子线程中运行,所以建立一个handler,将需要在子线程中运行的dialog放到主线程中的handler中运行。
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//导入android.os.Handler包
builder.setTitle("确认") ;
switch (msg.what) {
case 0:
builder.setMessage("发送失败") ;
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
break;
case 1:
builder.setMessage("发送成功") ;
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
break;
}
builder.create().show();
super.handleMessage(msg);
}
};
//======================================================================================================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendRequest = (Button) findViewById(R.id.send_request);//实例化按钮id
responseText = (TextView) findViewById(R.id.response);
sendRequest.setOnClickListener(this); //由于整个MainActivity继承了OnClickListener,所以可以直接调用button按钮的监听方法,当点击按钮时触发下面的onClick方法
}
@Override
public void onClick(View v) { //当点击id为send_request的控件时,调用sendRequestWithHttpURLConnection方法
if (v.getId() == R.id.send_request) {
sendRequestWithHttpClient();
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
//发送数据
private void sendRequestWithHttpClient() {
final String value1 = "123"; //定义要传输的数值
final String value2 = "123";
final String url="http://121.43.193.208/index.php/Home/Index/login"; //定义地址
NameValuePair nameValuePair1 = new BasicNameValuePair("account",value1);//建立“键值对”nameValuePair,存放post请求的参数 前面一个键名后面一个值
NameValuePair nameValuePair2 = new BasicNameValuePair("password",value2);
final List<NameValuePair>nameValuePairs = new ArrayList<NameValuePair>();//建立一个NameValuePair数组,用于存储欲传送的参数
nameValuePairs.add(nameValuePair1); //将“键值对”装入刚刚定义的数组集合对象
nameValuePairs.add(nameValuePair2);
//--------------------------------------------------------------------------------------------------------------------------------------
new Thread(new Runnable() { //开条线程
@Override
public void run() {
try {
HttpEntity requeshttpEntity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8); //必须加 HTTP.UTF_8,这样才不会乱码
Log.d("MainActivity",requeshttpEntity.toString());
HttpPost httpPost = new HttpPost(url);//此处还可用get方法传输代码为HttpGet httpGet = new HttpGet(url);当然,若用get方法,则下面的代码也得改
httpPost.setEntity(requeshttpEntity);//HttpEntity类键值对“添加进URL地址中”
HttpClient httpClient = new DefaultHttpClient(); //建立客户端对象
HttpResponse httpResponse = httpClient.execute(httpPost);//把“融合了键值对的URL地址”加到客户端中,得到HttpResponse对象
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
//接收数据
if (httpResponse.getStatusLine().getStatusCode() == 200) {// 如果请求和响应都成功了
HttpEntity entity = httpResponse.getEntity(); //通过“客户端对象”接收数据
String response = EntityUtils.toString(entity, "utf-8"); //把获得的entity数据以utf-8的编码转成String类型
Log.d("MainActivity(验证)",response); //断点,验证response是否获取到
if(response.equals("0")){
handler.sendEmptyMessage(0);//给主线程中的handler发送0
Log.e("False", response);
} else{
parseJSONWithJSONObject(response);//调用parseJSONWithJSONObject方法解析response数据(json)
handler.sendEmptyMessage(1);//给主线程中的handler发送1
Log.d("Success", response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
//解析json
private void parseJSONWithJSONObject(String jsonData) { //负责把得到的json数组循环输出
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String key1 = jsonObject.getString("type");
String key2 = jsonObject.getString("name");
String key3 = jsonObject.getString("pay");
String key4 = jsonObject.getString("place");
String key5 = jsonObject.getString("start");
String key6 = jsonObject.getString("number");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}