码迷,mamicode.com
首页 > 移动开发 > 详细

Android 子线程请求ASP.NET后台

时间:2015-01-13 19:23:59      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

首先定义布局文件,及点击事件

技术分享
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();
            }
        }
    }
}
View Code

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;
    }
}
View Code

 

Android 子线程请求ASP.NET后台

标签:

原文地址:http://www.cnblogs.com/cyccess/p/4221888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!