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_double/", nameValuePairs);int returncode =httpPostData("http://numberology.sinaapp.com/userdata/datasave_single/", nameValuePairs);
Android客户端向Python服务器以POST方式传输数据
最新推荐文章于 2022-10-27 21:28:22 发布