简体   繁体   中英

JSON fetching failing in android studio

I am new to android and am trying to get a json file from an api and seem to be getting errors. I do not know where the error is coming from. I created a new thread and in the run method have the code to get the data from the api. I have the response string being logged but an error keeps coming up instead.

This is the code to my main activity

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.Buffer;
import java.util.*;

public class MainActivity extends AppCompatActivity implements BookListFragment.OnFragmentInteractionListener {
    final ArrayList<Book> names = new ArrayList<Book>();
    private boolean twoPane = false;
    BookListFragment blf;
    BookDetailsFragment bdf;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        twoPane = findViewById(R.id.container2) == null;
        Context context = getApplicationContext();
        Resources res = context.getResources();
        final String searchString = "";
        Thread t = new Thread(){
            @Override
            public void run(){
                URL bookData;
                try{
                    bookData = new URL("https://kamorris.com/lab/audlib/booksearch.php?search="+searchString);
                    HttpURLConnection conn = (HttpURLConnection) bookData.openConnection();
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String response = "", tmpResponse;
                    tmpResponse =  br.readLine();
                    while(tmpResponse != null){
                        response= response + tmpResponse;
                        Log.d("response", tmpResponse);
                        tmpResponse = br.readLine();
                    }
                    Log.d("Response", response);
                    JSONObject bookObject = new JSONObject(response);
                    Message msg = Message.obtain();
                    msg.obj = bookObject;

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        /*if(twoPane){
            ViewPagerFragment vp = ViewPagerFragment.newInstance(names);
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.container1,vp);
            ft.commit();
        } else {
            blf = BookListFragment.newInstance(names);
            bdf = BookDetailsFragment.newInstance(names.get(0));
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.container1, blf);
            ft.replace(R.id.container2,bdf);
            ft.addToBackStack(null);
            ft.commit();
        }*/

    }

    Handler responseHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            JSONObject respObject = (JSONObject) msg.obj;

            return false;
        }
    });

    @Override
    public void onItemSelection(Book bookname) {
        bdf.updateBookName(bookname);
    }
}

and this is the error

2019-11-14 16:47:38.682 2848-12759/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
    com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
        at com.google.android.apps.gsa.staticplugins.microdetection.d.k.a(SourceFile:91)
        at com.google.android.apps.gsa.staticplugins.microdetection.d.l.run(Unknown Source:14)
        at com.google.android.libraries.gsa.runner.a.a.b(SourceFile:32)
        at com.google.android.libraries.gsa.runner.a.c.call(Unknown Source:4)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)
        at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
        at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
        at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)
     Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
        at com.google.android.apps.gsa.speech.audio.Tee.j(SourceFile:103)
        at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
        at java.io.InputStream.read(InputStream.java:101)
        at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:17)
        at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)

and this

2019-11-14 16:47:43.735 2848-12762/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
    com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
        at com.google.android.apps.gsa.staticplugins.microdetection.d.k.a(SourceFile:91)
        at com.google.android.apps.gsa.staticplugins.microdetection.d.l.run(Unknown Source:14)
        at com.google.android.libraries.gsa.runner.a.a.b(SourceFile:32)
        at com.google.android.libraries.gsa.runner.a.c.call(Unknown Source:4)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)
        at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
        at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
        at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)
     Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
        at com.google.android.apps.gsa.speech.audio.Tee.j(SourceFile:103)
        at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
        at java.io.InputStream.read(InputStream.java:101)
        at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:17)
        at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)

The correct version of the code should the the data is got and saved into the response string and saves into a JSONObject. The correct output should be for the response string to be logged in the log cat.

good day sir. you open a connection but you did not connect to the url. try typing conn.connect()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM