-1

i got a class i made, that makes a simple GET request and displays toast messege with the response...

if i call the function from a bottom event click it displays the messege with the data returned, just ok.

but if i call the same function from a brodcast reciver class, it just showing the massege with the data '' (null)

i belive that it showing the massege before i could get the data, and with the button it waits for the data..

how can i make it work from the brodcast reciver?

the class:

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;

 } 
 }

from the button it works fine:

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

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

 }});

from the brodcast reciver witch in different class it won't return data, but shows the toast..

public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

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


    myclass.doHttpRequest("blabla", context);


    break;
   } }


    }

please HELP.... :) THANKS

itai
  • 302
  • 5
  • 15

2 Answers2

0

All fixed.. it's seems to be a project settings error, or premmisions error (but i had the premissions in the manifest... must not apply in the apk while compiling)

i copied the classes to enother project made by erlier version of eclipse, and it's works there like a charm...

thanks anyway.. i tried for hours to fix it in the code.. and it was the settings....

itai
  • 302
  • 5
  • 15
  • Tip: `public abstract void onReceive (Context context, Intent intent)` This method is always called within the main thread of its process, unless you explicitly asked for it to be scheduled on a different thread using registerReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler). When it runs on the main thread you should never perform long-running operations in it. Source : http://developer.android.com/reference/android/content/BroadcastReceiver.html – Vishal Vyas Sep 24 '12 at 00:34
0

I created a myclass.java and CustomBroadcastReceiver.java and tried your code by removing the break statement and one extra curly brace from the CustomBroadcastReceiver.java class and it worked fine for me.

The following code in Activity class demonstrates registering, uninteresting receiver and a Handler for a sample/test broadcast.

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

Hope this helps.

Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39