skip to Main Content

Java code should download source code from facebook graph api, but is not working. Code output is htmlCode = ""
This is java code:

package cz.apps;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    public String htmlCode = "";

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

    public void ziskatdata(){
        try {
            URL url = new URL("https://graph.facebook.com/v2.8/****/events?access_token=****");
           BufferedInputStream bis = new BufferedInputStream(url.openStream());
           byte[] buffer = new byte[1024];
           StringBuilder sb = new StringBuilder();
           int bytesRead = 0;
           while((bytesRead = bis.read(buffer)) > 0) {
               String text = new String(buffer, 0, bytesRead);
               sb.append(text);
           }
           bis.close();

         htmlCode = sb.toString();
        } catch (Exception e) {
            //Data se nestáhla
        }

        if (htmlCode.equals("")){
            //Nezdařilo se vypsat události
        } else {
            try {
                JSONObject json = new JSONObject(htmlCode);
                JSONArray jarray = json.getJSONArray("data");
                for(int i = 0; i < jarray.length(); i++){
                    JSONObject udalosti = jarray.getJSONObject(i);
                    String popis = udalosti.getString("description");
                    String konec = udalosti.getString("end_time");
                    String zacatek = udalosti.getString("start_time");
                    String jmeno = udalosti.getString("name");

                    JSONObject lokace = udalosti.getJSONObject("place").getJSONObject("location");
                    String mesto = lokace.getString("city");
                    String zeme = lokace.getString("country");
                    String lat = lokace.getString("latitude");
                    String lon = lokace.getString("longitude");
                    final TextView textViewToChange = (TextView) findViewById(R.id.textprovypsani);
                    textViewToChange.setText(mesto);
                }
            } catch (JSONException e) {
                //Něco je špatně
            }
        }
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cz.app" >
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

catch (Exception e) { Log.e("MYAPP", "exception", e); } LogCat output:

E/MYAPP   (21057): exception
E/MYAPP   (21057): android.os.NetworkOnMainThreadException

2

Answers


  1. You have to use this code not in mainUI Thread. So,

    new Thread(new Runnable() {
        public void run() {
            try {
                ziskatdata();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    

    and inside your method update all UI view using this:

    runOnUiThread(new Runnable() {
        public void run() {
            try {
                textViewToChange.setText(mesto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    
    Login or Signup to reply.
  2. The error says that you are trying to make a network call on the main thread of the application, which is not permitted. You need to spawn another thread to make a network call. You can do something like:

    new Thread() {
        @Override
        public void run() {
            // do your work here
        }
    }.start();
    

    But you cannot do anything related to UI on that thread. You can also use a class like AsyncTask:

    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String response = whatEverMethodGetsMeNetworkCallResponse();
    
            return response;
        }
    
        @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);
    
            // Do whatever you want to do with the network response
        }
    }.execute();
    

    You can probably use a helper class to access web APIs, like one of mine: Network.java

    It will handle all the complications of reading and writing streams, you can make a call to a URL, and get the response as a string or a Stream if you are expecting a binary file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search