网友通过本文主要向大家介绍了android http请求,android http请求框架,android http请求头,android http get请求,android http网络请求等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
android http同步请求,android同步请求
1、界面
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:orientation="vertical" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity" > 7 8 <EditText 9 android:id="@+id/et_username" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:hint="请输入用户名" 13 android:text="张三" 14 /> 15 16 17 <EditText 18 android:id="@+id/et_password" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:hint="请输入密码" 22 android:inputType="textPassword" /> 23 24 <Button 25 android:onClick="myGetData" 26 android:id="@+id/button1" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="登陆" /> 30 31 </LinearLayout>
2、MainActivity代码,用来响应button代码
1 package com.example.getdata; 2 3 import java.net.HttpURLConnection; 4 import java.net.MalformedURLException; 5 import java.net.URL; 6 7 import com.example.getdata.service.LoginService; 8 9 import android.os.Bundle; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.app.Activity; 13 import android.view.Menu; 14 import android.view.View; 15 import android.widget.EditText; 16 import android.widget.Toast; 17 18 public class MainActivity extends Activity { 19 20 private EditText et_username; 21 private EditText et_password; 22 /*private Handler handler = new Handler(){ 23 24 @Override 25 public void handleMessage(android.os.Message msg) { 26 // TODO Auto-generated method stub 27 28 } 29 30 };*/ 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 et_username = (EditText)findViewById(R.id.et_username); 36 et_password = (EditText)findViewById(R.id.et_password); 37 } 38 39 40 public void myGetData(View view){ 41 final String username = et_username.getText().toString().trim(); 42 final String password = et_password.getText().toString().trim(); 43 System.out.println("username:" + username); 44 System.out.println("password:" + password); 45 new Thread(){ 46 public void run(){ 47 //final String result = LoginService.loginByGet(username, password); 48 //final String result = LoginService.loginByPost(username, password); 49 //final String result = LoginService.loginByClientGet(username, password); 50 final String result = LoginService.loginByClientPost(username, password); 51 if(result != null){ 52 runOnUiThread(new Runnable(){ 53 @Override 54 public void run() { 55 // TODO Auto-generated method stub 56 Toast.makeText(MainActivity.this, result, 0).show(); 57 } 58 59 }); 60 }else{ 61 runOnUiThread(new Runnable(){ 62 @Override 63 public void run() { 64 // TODO Auto-generated method stub 65 Toast.makeText(MainActivity.this, "请求不成功!", 0).show(); 66 } 67 68 }); 69 } 70 }; 71 }.start(); 72 } 73 74 }
3、http请求同步代码
1 package com.example.getdata.service; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.io.UnsupportedEncodingException; 7 import java.net.HttpURLCo