package org.anjoy.demo;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class PostsaxActActivity extends Activity {
private TextView txtname;
private TextView txtage;
private Button submit;
final String path="http://172.16.10.174:3002/api3.xml?key=9fc1838ecd16cbed7d550aa21ae72587&args=%3Cil%3E%3Ci%20n=%27test%27%20v=%273.1%27%3E%3Cshenfenzheng%3Esdf%3C/shenfenzheng%3E%3C/i%3E%3C/il%3E&deviceid=6f68bd73dd79f7c273905954eb85c38bf673affe&deviceid=352540040749946";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtname = (TextView)findViewById(R.id.txtname);
txtage = (TextView)findViewById(R.id.txtage);
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = txtname.getText().toString();
String age = txtage.getText().toString();
String url1=path+"&name="+name+"&age="+age;
Map<String,String> map1=new HashMap<String, String>();
map1.put("name", name);
map1.put("age",age);
try {
//submitDataByDoPost(map1, url1);
getWeather();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Boolean submitDataByDoPost(Map<String, String> map, String path) throws Exception {
//获取控件的值
// 注意Post地址中是不带参数的,所以newURL的时候要注意不能加上后面的参数
URL Url = new URL(path);
// Post方式提交的时候参数和URL是分开提交的,参数形式是这样子的:name=y&age=6
StringBuilder sb = new StringBuilder();
// sb.append("?");
for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue());
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
String str = sb.toString();
HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection();
HttpConn.setRequestMethod("POST");
HttpConn.setReadTimeout(5000);
HttpConn.setDoOutput(true);
HttpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
HttpConn.setRequestProperty("Content-Length", String.valueOf(str.getBytes().length));
OutputStream os = HttpConn.getOutputStream();
os.write(str.getBytes());
if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
Toast.makeText(PostsaxActActivity.this, "提交服务器数据成功!", Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(PostsaxActActivity.this, "提交服务器数据失败!", Toast.LENGTH_LONG).show();
return false;
}
private void getWeather(){
try {
final String SERVER_URL = "http://172.16.10.174:3002/api3.xml?key=9fc1838ecd16cbed7d550aa21ae72587&args=%3Cil%3E%3Ci%20n=%27test%27%20v=%273.1%27%3E%3Cshenfenzheng%3Esdf%3C/shenfenzheng%3E%3C/i%3E%3C/il%3E&deviceid=6f68bd73dd79f7c273905954eb85c38bf673affe&deviceid=352540040749946"; // 定义需要获取的内容来源地址
HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求
List params = new ArrayList();
params.add(new BasicNameValuePair("name", txtname.getText().toString())); // 添加必须的参数
params.add(new BasicNameValuePair("age", txtage.getText().toString())); // 添加必须的参数
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数的编码
HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 发送请求并获取反馈
// 解析返回的内容
if (httpResponse.getStatusLine().getStatusCode() != 404)
{
String result = EntityUtils.toString(httpResponse.getEntity());
Toast.makeText(PostsaxActActivity.this, "提交服务器数据成功!", Toast.LENGTH_LONG).show();
System.out.println(result);
}
} catch (Exception e) {
Toast.makeText(PostsaxActActivity.this, "提交服务器数据失败!", Toast.LENGTH_LONG).show();
}
}
}