Android客户端向Python服务器以POST方式传输数据

import java.util.ArrayList;
import java.util.List;

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 android.util.Log;

import com.baidu.location.BDLocation;
import com.bbchen.data.UserInfo;

public class Send2Web {

	public static void Send2Sina_Single(HardwareInfo hardwareInfo, BDLocation location, UserInfo userInfo) {
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("name", userInfo.GetName() == null ? " " : userInfo.GetName()));
		nameValuePairs.add(new BasicNameValuePair("sex", ""+userInfo.GetSex() == null ? "0" : ""+userInfo.GetSex()));
		nameValuePairs.add(new BasicNameValuePair("birthday", userInfo.GetBirthDay() == null ? "2013-01-01" : userInfo.GetBirthDay()));
		nameValuePairs.add(new BasicNameValuePair("test_date", location.getTime() == null ? "2013-01-01 08:00:00" : location.getTime()));		
		nameValuePairs.add(new BasicNameValuePair("software_version", hardwareInfo.software_version == null ? " " : hardwareInfo.software_version));
		nameValuePairs.add(new BasicNameValuePair("device", hardwareInfo.device == null ? " " : hardwareInfo.device));
		nameValuePairs.add(new BasicNameValuePair("system_version", hardwareInfo.system_version == null ? " " : hardwareInfo.system_version));
		nameValuePairs.add(new BasicNameValuePair("imei", hardwareInfo.imei == null ? " " : hardwareInfo.imei));
		nameValuePairs.add(new BasicNameValuePair("isocode", hardwareInfo.isoCodeString == null ? " " : hardwareInfo.isoCodeString));
		nameValuePairs.add(new BasicNameValuePair("city", location.getCity() == null ? " " : location.getCity()));
		nameValuePairs.add(new BasicNameValuePair("district", location.getDistrict() == null ? " " : location.getDistrict()));
		nameValuePairs.add(new BasicNameValuePair("latitude", location.getLatitude() < -1000 ? "0" : ""+location.getLatitude()));
		nameValuePairs.add(new BasicNameValuePair("longitude", location.getLongitude() < -1000 ? "0" : ""+location.getLongitude()));
		nameValuePairs.add(new BasicNameValuePair("cgi", hardwareInfo.sCell == null || hardwareInfo.sCell.getCell() == null ? "0" : hardwareInfo.sCell.getCell()));
		nameValuePairs.add(new BasicNameValuePair("ip_address", hardwareInfo.ipAddress_net == null ? "0.0.0.0" : hardwareInfo.ipAddress_net));
		nameValuePairs.add(new BasicNameValuePair("networking", hardwareInfo.networking == null ? " " : hardwareInfo.networking));
		nameValuePairs.add(new BasicNameValuePair("operators", hardwareInfo.operatorString == null ? " " : hardwareInfo.operatorString));
		int returncode =httpPostData("http://number.sinaapp.com", nameValuePairs);
		Log.i("http_post_return", "code:"+returncode);
	}
	
	public static void Send2Sina_Double(HardwareInfo hardwareInfo, BDLocation location, UserInfo userInfo1, UserInfo userInfo2) {
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("name_boy", userInfo1.GetName() == null ? " " : userInfo1.GetName()));
		nameValuePairs.add(new BasicNameValuePair("birthday_boy", userInfo1.GetBirthDay() == null ? "2013-01-01" : userInfo1.GetBirthDay()));
		nameValuePairs.add(new BasicNameValuePair("name_girl", userInfo2.GetName() == null ? " " : userInfo2.GetName()));
		nameValuePairs.add(new BasicNameValuePair("birthday_girl", userInfo2.GetBirthDay() == null ? "2013-01-01" : userInfo2.GetBirthDay()));
		nameValuePairs.add(new BasicNameValuePair("test_date", location.getTime() == null ? "2013-01-01 08:00:00" : location.getTime()));		
		nameValuePairs.add(new BasicNameValuePair("software_version", hardwareInfo.software_version == null ? " " : hardwareInfo.software_version));
		nameValuePairs.add(new BasicNameValuePair("device", hardwareInfo.device == null ? " " : hardwareInfo.device));
		nameValuePairs.add(new BasicNameValuePair("system_version", hardwareInfo.system_version == null ? " " : hardwareInfo.system_version));
		nameValuePairs.add(new BasicNameValuePair("imei", hardwareInfo.imei == null ? " " : hardwareInfo.imei));
		nameValuePairs.add(new BasicNameValuePair("isocode", hardwareInfo.isoCodeString == null ? " " : hardwareInfo.isoCodeString));
		nameValuePairs.add(new BasicNameValuePair("city", location.getCity() == null ? " " : location.getCity()));
		nameValuePairs.add(new BasicNameValuePair("district", location.getDistrict() == null ? " " : location.getDistrict()));
		nameValuePairs.add(new BasicNameValuePair("latitude", location.getLatitude() < -1000 ? "0" : ""+location.getLatitude()));
		nameValuePairs.add(new BasicNameValuePair("longitude", location.getLongitude() < -1000 ? "0" : ""+location.getLongitude()));
		nameValuePairs.add(new BasicNameValuePair("cgi", hardwareInfo.sCell == null || hardwareInfo.sCell.getCell() == null ? " " : hardwareInfo.sCell.getCell()));
		nameValuePairs.add(new BasicNameValuePair("ip_address", hardwareInfo.ipAddress_net == null ? "0.0.0.0" : hardwareInfo.ipAddress_net));
		nameValuePairs.add(new BasicNameValuePair("networking", hardwareInfo.networking == null ? " " : hardwareInfo.networking));
		nameValuePairs.add(new BasicNameValuePair("operators", hardwareInfo.operatorString == null ? " " : hardwareInfo.operatorString));
		int returncode =httpPostData("http://number.sinaapp.com/", nameValuePairs);
		Log.i("http_post_return", "code:"+returncode);
	}
	
	// 1. 使用POST方式时,传递参数必须使用NameValuePair数组
	// 2. 使用GET方式时,通过URL传递参数,注意写法
	// 3. 通过setEntity方法来发送HTTP请求
	// 4. 通过DefaultHttpClient 的 execute方法来获取HttpResponse
	// 5. 通过getEntity()从Response中获取内容
	public static int httpPostData(String urlString,List<NameValuePair> nameValuePairs){
          
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(urlString);
          try{
                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
                  HttpResponse response = httpclient.execute(httppost);
                  int statusCode = response.getStatusLine().getStatusCode();
                  return statusCode;
          }catch(Exception e){
                  e.printStackTrace();
          }
          return 0;

	}

}
int returncode =httpPostData("http://numberology.sinaapp.com/userdata/datasave_single/", nameValuePairs);
int returncode =httpPostData("http://numberology.sinaapp.com/userdata/datasave_double/", nameValuePairs);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值