标签:
首先定义布局文件,及点击事件
public class MainActivity extends Activity { private final int MSG_HELLO = 0; private Handler mHandler; private Button btnSubmit; private EditText txtUsername, txtPassword; private TextView loginResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSubmit = (Button) findViewById(R.id.btnSubmit); txtUsername = (EditText) findViewById(R.id.username); txtPassword = (EditText) findViewById(R.id.password); loginResult = (TextView) findViewById(R.id.loginResult); btnSubmit.setOnClickListener(new OnClickLoginListener()); } class OnClickLoginListener implements View.OnClickListener { @Override public void onClick(View view) { String username = txtUsername.getText().toString(); String password = txtPassword.getText().toString(); // 登录请求地址 String url = "http://172.16.18.10:8080/Home/Login"; // 参数封装 Map<String ,String> params = new HashMap<String, String>(); params.put("username",username); params.put("password",password); try { // 请求登录 String result = HttpUtils.postRequest(url, params); // 讲请求结果转换成 JSON 对象 JSONObject jsonObject = new JSONObject(result); String message = jsonObject.getString("message"); int status = jsonObject.getInt("status"); // 登录成功 if(status == 1) { Log.i("Login","登录成功!"); Intent it = new Intent(MainActivity.this, MainActivity2.class); Bundle bundle = new Bundle(); bundle.putString("name", message+",username:"+ username +" password:"+password); it.putExtras(bundle); // it.putExtra(“test”, "mobile”); startActivity(it); // startActivityForResult(it,REQUEST_CODE); finish(); } else { loginResult.setText(message); } } catch (Exception e) { e.printStackTrace(); } } } }
HttpUtils
public class HttpUtils { /* * @function HttpPost 请求 * @param params 请求参数 * @return 服务器响应字符串 * @throws Exception */ public static String postRequest(final String url, final Map<String,String> rawParams ) throws Exception { FutureTask<String> task = new FutureTask<String>(new Callable<String>() { @Override public String call() throws Exception { // 创建一个默认的HTTP客户端 DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建 HttpPost 对象 HttpPost post = new HttpPost(url); // 如果传递参数个数比较多,可以对传递的参数进行封装 List<NameValuePair> params = new ArrayList<NameValuePair>(); for (String key : rawParams.keySet()) { // 封装请求参数 params.add(new BasicNameValuePair(key,rawParams.get(key))); } // 设置请求参数 post.setEntity(new UrlEncodedFormEntity(params, "gbk")); // 发送 POST 请求 HttpResponse httpResponse = httpClient.execute(post); // 如果服务器成功地返回响应 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(httpResponse.getEntity()); return result; } return null; } }); new Thread(task).start(); return task.get(); } /* * @function 下载一个图片 * @param imgPath 图片下载地址 * @return Bitmap 对象 * @throws Exception */ public static Bitmap DownloadImage(String imgPath) throws Exception { Bitmap bitmap = null; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(imgPath); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { byte[] data = EntityUtils.toByteArray(httpResponse.getEntity()); // 得到一个Bitmap对象,并且为了使其在post内部可以访问,必须声明为final bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return bitmap; } }
标签:
原文地址:http://www.cnblogs.com/cyccess/p/4221888.html