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

使用POST方式提交数据时的中文乱码解决方法(重点)

解决办法:使用客户端和服务器两边的字符集编码保持一致。
UTF-8,

使用GET方式提交数据的中文乱码的解决方法 使用URLEncoder.encode(name,"UTF-8")进行url编码: String path = "http://192.168.22.136:8080/web/servlet/LoginServlet?username="+URLEncoder.encode(name,"UTF-8")+"&password="+URLEncoder.encode(pwd,"UTF-8"); 使用httpclient提交数据(重点)

apache httpClient 轻量级的浏览器:

使用GET方式数据步骤:

1、创建一个浏览器:

2、输入一个网址:

3、敲回车

模块代码:

package com.itheima.qqlogin; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import com.itheima.htmlview.utils.StreamTools; 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_pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); 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{ new Thread(){ public void run() { try { String path = "http://192.168.22.136:8080/web/servlet/LoginServlet?username="+URLEncoder.encode(name,"UTF-8")+"&password="+URLEncoder.encode(pwd,"UTF-8"); // // 1、创建一个浏览器: HttpClient client = new DefaultHttpClient(); // 2、输入一个网址: HttpGet httpGet = new HttpGet(path); // // 3、敲回车 HttpResponse response = client.execute(httpGet); //获取服务器端返回的响应码(状态码) int code = response.getStatusLine().getStatusCode(); // int code = conn.getResponseCode(); if(code == 200){ // 4、解析服务器返回的二进制数据,解析成一个字符串 // InputStream is = conn.getInputStream(); //获取服务器返回的二进制数据流 InputStream is = response.getEntity().getContent(); String result = StreamTools.readStream(is); System.out.println("---------"+ result); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } }

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

1、创建一个浏览器:

2、输入一个网址:

3、敲回车

模版代码:

package com.itheima.qqlogin; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; 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 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; 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{ new Thread(){ public void run() { try { String path = "http://192.168.1.196: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、创建一个浏览器: HttpClient client = new DefaultHttpClient(); // 2、输入一个网址: HttpPost httpPost = new HttpPost(path); List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); BasicNameValuePair p1 = new BasicNameValuePair("username", name); BasicNameValuePair p2 = new BasicNameValuePair("password", pwd); parameters.add(p1); parameters.add(p2); //默认使用iso-8859-1编码,需要指定UTF-8 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"UTF-8"); // 3、敲回车 //设置提交的数据 httpPost.setEntity(entity); HttpResponse response = client.execute(httpPost); // 4、服务器返回数据 // (1)判断状态码:200 ok,404 没有找到资源、503、509 服务器端错误 int code = response.getStatusLine().getStatusCode(); // int code = conn.getResponseCode(); if(code == 200){ // (2)解析服务器返回的二进制数据,解析成一个字符串 InputStream is = response.getEntity().getContent(); String result = StreamTools.readStream(is); System.out.println("---------"+ result); Message msg = Message.obtain(); msg.obj = result; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } } 使用Aynchttpclient向服务器端提交数据(重点)

1、创建一个浏览器:

2、输入一个网址:

3、敲回车

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

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