1首先建立一个activity:
Mainactivity.java文件
package com.example.httpclicentcallback;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView1;
private Button button1;
private EditText editText1;
private EditText editText2;
private static String result="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
textView1.setText(result);
break;
default:
break;
}
};
};
public void initView(){
textView1=(TextView) findViewById(R.id.textView1);
button1=(Button) findViewById(R.id.button1);
editText1=(EditText) findViewById(R.id.editText1);
editText2=(EditText) findViewById(R.id.editText2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
final String url="你的服务器ip"+"username="+editText1.getText().toString()+"&password="+editText2.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
new HttpClicentTest().compute(url, new HttpCallBackTest() {
@Override
public void onSuccess(String result) {
Log.e("TAG", result);
MainActivity.result=result;
Message msg=new Message();
msg.what=0;
handler.sendMessage(msg);
//textView1.setText(result);本来我是在这里更新ui的没想到直接报ui线程更新异常,没事咱们用handle更新ui就是了
}
});
}
}).start();
}
});
}
}
xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="39dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="45dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="78dp"
android:text="TextView" />
</RelativeLayout>
2 、定义一个接口:
public interface HttpCallBackTest {
public void onSuccess(String result);
}
3、建立回调机制
package com.example.httpclicentcallback;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
public class HttpClicentTest {
public void compute(String url,HttpCallBackTest callback){
callback.onSuccess(getHttpContext(url));
}
public String getHttpContext(String ul){
String restlt="";
URL url=null;
HttpURLConnection connection=null;
InputStreamReader in=null;
try{
url = new URL(ul);
connection=(HttpURLConnection) url.openConnection();
in=new InputStreamReader(connection.getInputStream());
BufferedReader bufferedReader=new BufferedReader(in);
StringBuffer strBuffer=new StringBuffer();
String line=null;
while((line=bufferedReader.readLine())!=null){
strBuffer.append(line);
}
restlt=strBuffer.toString();
}catch (Exception e){
e.printStackTrace();
}finally{
if(connection!=null){
connection.disconnect();
}
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return restlt;
}
class MyAnsyTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... arg0) {
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
}