-1

我做了一个类,它发出一个简单的 GET 请求并显示带有响应的 toast messege ...

如果我从底部事件单击调用该函数,它会显示带有返回数据的消息,就可以了。

但是如果我从广播接收器类中调用相同的函数,它只会显示带有数据''(null)的masege

我相信它会在我获取数据之前显示按摩,并使用按钮等待数据..

我怎样才能让它从广播接收器工作?

班上:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class myclass{


public static void doHttpRequest(String param1, Context context){

    String ret="";

     try {
         URL url = new URL("http://website.com/page?param="+param1);
        HttpURLConnection con = (HttpURLConnection) url
            .openConnection();
        ret=readStream(con.getInputStream());
        } catch (Exception e) {
        e.printStackTrace();
    }

    CharSequence text = "return data-  "+ret;
    int duration = Toast.LENGTH_LONG;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();   
}



  private static String readStream(InputStream in) {
BufferedReader reader = null;
String line = "";
String alllines = "";
try {
    reader = new BufferedReader(new InputStreamReader(in));

    while ((line = reader.readLine()) != null) {
    alllines=alllines+line;
    }

    return alllines;

} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) {
      try {
        reader.close();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }

}
return alllines;

 } 
 }

从按钮它工作正常:

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

 myclass.doHttpRequest("blabla", getBaseContext());

 }});

来自不同班级的广播接收者女巫,它不会返回数据,但会显示吐司..

public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {


    myclass.doHttpRequest("blabla", context);


    break;
   } }


    }

请帮助.... :) 谢谢

4

2 回答 2

0

全部修复..这似乎是一个项目设置错误,或者premmisions错误(但我在清单中有权限......编译时不能在apk中应用)

我将这些类复制到了由 Eclipse 的 erlier 版本制作的另一个项目中,它在那里的工作就像一个魅力......

无论如何,谢谢..我尝试了几个小时在代码中修复它..这是设置....

于 2012-09-24T00:23:20.897 回答
0

我创建了一个myclass.javaCustomBroadcastReceiver.java通过从类中删除 break 语句和一个额外的花括号来尝试您的代码,CustomBroadcastReceiver.java它对我来说效果很好。

Activity 类中的以下代码演示了示例/测试广播的注册、无趣的接收器和处理程序。

CustomBroadcastReceiver customBroadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    customBroadcastReceiver = new CustomBroadcastReceiver();
    registerReceiver(cusoBroadcastReceiver, new IntentFilter("com.example.app.testbroadcast"));

            // For test broadcast only.
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            sendBroadcast(new Intent("com.example.app.testbroadcast"));
        }
    }, 2000);

}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    unregisterReceiver(customBroadcastReceiver);
} 

希望这可以帮助。

于 2012-09-24T00:26:51.087 回答