-1

i have an activity like message box and i want to get jason response of a php page: my code is something like this but the app crashes! here are my codes ! i dont know what happens but in my stack just writes 'null'

    public class MyJason {

private static final String LOGGER = null;
private String myurl;
public MyJason(String requesturl )
{

    URL searhURL;
    this.myurl=requesturl;


}
public JSONObject returnjason()
{

     URL url = null;
    try {
        url = new URL(myurl);
    } catch (MalformedURLException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
        InputStream is = null;

          URLConnection connection = null;
        try {
            connection = url.openConnection();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
          Log.d(LOGGER, "1.0");
            String line;
            StringBuilder builder = new StringBuilder();
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(
                                connection.getInputStream()));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } //problem is here
            Log.d(LOGGER, "2.0");
            try {
                while ((line = reader.readLine()) != null) {
                        builder.append(line);
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Log.d(LOGGER, "3.0");

             String jSONString = builder.toString();
            //after cutting off the junk, its ok
            Log.d(LOGGER, "String is : "+jSONString);
            JSONObject jSONObject = null;
            try {
                jSONObject = new JSONObject(jSONString);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } //whole json object
            return jSONObject;


}

and the way i call them is :

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_box);
    MyJason res=new MyJason("http://localhost/skyniaz/test.php"); 
    TextView s=(TextView)findViewById(R.id.messagebox_text); 
    s.setText(res.returnjason().toString());
}

i edited my activity like this: is this correct????

    s.setText(new MyJason().execute("http://10.0.2.2/skyniaz/test.php").get().getJSONArray("a").toString());

this is my php source

1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>
  • is your url returns JsonObject ?? – Ranjit Jan 14 '14 at 11:37
  • copy some content from your http://localhost/skyniaz/test.php page from desktop and paste here. i am sure it never returns JsonObject but only a html page which your code unable to parse and also check your connection is perfect or not through getStatusCode() (200) . – Ranjit Jan 14 '14 at 11:59

1 Answers1

0

You should make your requests with async tasks or Threads. UI thread can't wait your request answer and lock your screen.

Barışcan Kayaoğlu
  • 1,294
  • 3
  • 14
  • 35