安卓基础干货(五):安卓线程与数据上传下载的学习 (2)

使用GET方式向服务器端提交数据

package com.itheima.qqlogin; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import org.apache.http.Header; import com.itheima.htmlview.utils.StreamTools; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name; private EditText et_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = (EditText) findViewById(R.id.et_qq); et_pwd = (EditText) findViewById(R.id.et_pwd); } public void login(View view){ final String name = et_qq.getText().toString().trim(); final String pwd = et_pwd.getText().toString().trim(); if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){ Toast.makeText(this, "qq和密码不能空", 0).show(); return; }else{ try { String path = "http://192.168.1.103:8080/web/servlet/LoginServlet?username="+URLEncoder.encode(name,"UTF-8")+"&password="+URLEncoder.encode(pwd,"UTF-8"); //1、创建一个浏览器: AsyncHttpClient client = new AsyncHttpClient(); //2、输入网址,调用get方式向服务器发送请求: client.get(path, new AsyncHttpResponseHandler() { /* * 请求成功,服务器端正常响应 响应码200 * statusCode 响应码200ok,404没有找到资源,503服务器内部错误, Header[] headers 响应头信息, byte[] responseBody 服务器返回的数据 */ @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, new String(responseBody), 0).show(); } /* * 请求成功,服务器端正常响应 响应码200 * int statusCode, Header[] headers, byte[] responseBody, Throwable error * statusCode 响应码200ok,404没有找到资源,503服务器内部错误, Header[] headers 响应头信息, byte[] responseBody 服务器返回的数据 * Throwable error 服务器端返回的异常 */ @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable arg3) { Toast.makeText(MainActivity.this, new String(responseBody), 0).show(); } }); } catch (Exception e) { e.printStackTrace(); } }; } }

使用POST方式向服务器端提交数据

package com.itheima.qqlogin; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import org.apache.http.Header; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.itheima.htmlview.utils.StreamTools; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; public class MainActivity extends Activity { private EditText et_qq; private EditText et_pwd; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { String result = (String) msg.obj; Toast.makeText(MainActivity.this, result, 0).show(); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = (EditText) findViewById(R.id.et_qq); et_pwd = (EditText) findViewById(R.id.et_pwd); } public void login(View view){ final String name = et_qq.getText().toString().trim(); final String pwd = et_pwd.getText().toString().trim(); if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){ Toast.makeText(this, "qq和密码不能空", 0).show(); return; }else{ try { String path = "http://192.168.22.136:8080/web/servlet/LoginServlet"; // String data = "username="+name+"&password="+pwd; // String data = "username="+URLEncoder.encode(name,"UTF-8")+"&password="+URLEncoder.encode(pwd,"UTF-8"); // 1、创建一个浏览器: AsyncHttpClient client = new AsyncHttpClient(); // 2、输入一个网址,敲回车: RequestParams params = new RequestParams(); params.put("username", name); params.put("password", pwd); client.post(path, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, new String(responseBody), 0).show(); } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable e) { Toast.makeText(MainActivity.this, new String("服务器端返回数据失败"), 0).show(); } }); } catch (Exception e) { e.printStackTrace(); } } } }

多线程下载的原理 javase实现多线程

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwxxp.html