0

I am developing a new application, and it needs to get a JSON object from my website in the url http://10.0.0.8/mybiren/json.php (I'm aware that it's a local ip, it's just for testing the app).
I've got the function that gets the content of the site, and it have worked on a regular Java project, but when I tried it on my Android aplication project it have failed. when I've debugged the app in order to find where it stops, it have stopped on the line that calls yc.getInputStream() (yc is a URLConnection object). My friend said that I must add <uses-permission android:name="android.permission.INTERNET"/> to my AndroidManifest.xml but it didn't help.
Stack Trace doesn't show anything special


Java code

package com.example.arghh_mybiren;

import android.support.v7.app.ActionBarActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void getJson(View v)
    {
        URL u;
        try {
            u = new URL("http://10.0.0.8/mybiren/json.php");

        URLConnection yc = u.openConnection();
        yc.setDoInput(true);
        yc.setDoOutput(true);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                yc.getInputStream())); //debugger crashed here
        String inputLine;
        StringBuilder a = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
            a.append(inputLine);
        in.close();

        Toast.makeText(getApplicationContext(), a.toString(), Toast.LENGTH_SHORT).show();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}


XML code

<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="com.example.arghh_mybiren.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="getJson"
        android:onClick="getJson" />

</RelativeLayout>


Stack Trace

11-04 19:59:26.452: I/PGA(17170): New SOCKET connection: com.example.mybiren (pid 17170, tid 17170)
11-04 19:59:26.452: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0)
11-04 19:59:26.462: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0) returned
11-04 19:59:26.472: D/OpenGLRenderer(17170): Enabling debug mode 0
John Smith
  • 23
  • 1
  • 6
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 04 '15 at 17:55

1 Answers1

2

I really dont know what was wrong, but my friend sucseeded in helping me and sent me the following code (that have worked to me):

Java code

package com.example.arghh_mybiren;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private class MyTask extends AsyncTask<String, Void, String> 
    {
        @Override
        protected String doInBackground(String ... urls) 
        {
            try
            {
                URL url = new URL(urls[0]);
                URLConnection uc = url.openConnection();
                //String j = (String) uc.getContent();
                uc.setDoInput(true);
                BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
                String inputLine;
                StringBuilder a = new StringBuilder();
                while ((inputLine = in.readLine()) != null)
                    a.append(inputLine);
                in.close();

                return a.toString();
            }
            catch (Exception e)
            {
                return e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) 
        {
            Toast.makeText(getApplication(), result, Toast.LENGTH_LONG).show();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button loadBtn = (Button) this.findViewById(R.id.btn_load);
        loadBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Load();

            }
        });
    }
    public void Load()
    {
        Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
        MyTask taskLoad = new MyTask();
        taskLoad.execute("http://10.0.0.8/mybiren/JSON.php?day=Monday");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

XML code

<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="com.example.arghh_mybiren.MainActivity" >

    <Button
        android:id="@+id/btn_load"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="getJson"
        android:onClick="getJson" />

</RelativeLayout>


thanks to all who tried to help me.
John Smith
  • 23
  • 1
  • 6