Java、Android中的回调(callBack)我写的一个网络回调大家可以参考参考

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();
} 
}


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值