Ошибка отображается после реализации imagerequest - PullRequest
0 голосов
/ 20 сентября 2018

Я практикую библиотеку волейбола, но получаю некоторые ошибки.Я пытался с индивидуальным запросом.Это мой основной класс активности:

package in.jimmytrivedi.volleytutorials;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;

public class MainActivity extends AppCompatActivity {

    String server_url = "http://192.168.1.192/greetings.php";
    String image_server_url = "http://192.168.1.192/jimmytrivedi.jpeg";
    private Button getServerData, getServerImage;
    private TextView serverResponse;
    private ImageView serverImage;
    private RequestQueue requestQueue;

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

        getServerData = findViewById(R.id.getServerData);
        getServerImage = findViewById(R.id.getServerImage);
        serverResponse = findViewById(R.id.serverResponse);
        serverImage = findViewById(R.id.serverImage);

        /*-----Default New Request Que-----*/

        /*getServerData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
                StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                serverResponse.setText(response);
                                requestQueue.stop();
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        serverResponse.setText("Something went wrong");
                        error.printStackTrace();
                        requestQueue.stop();
                    }
                });

                requestQueue.add(stringRequest);
            }
        });*/

        /*-----Customized Request Que-----*/

        Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        requestQueue = new RequestQueue(cache, network);
        requestQueue.start();

        getServerData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                serverResponse.setText(response);
                                requestQueue.stop();
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        serverResponse.setText("Something went wrong");
                        error.printStackTrace();
                        requestQueue.stop();
                    }
                });


                Singleton.getmInstance(getApplicationContext()).addToRequestQueue(stringRequest);
            }
        });

        getServerImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageRequest imageRequest = new ImageRequest(image_server_url,
                        new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap response) {
                                serverImage.setImageBitmap(response);
                            }
                        }, 0, 0, ImageView.ScaleType.CENTER, null,
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
                                error.printStackTrace();
                                requestQueue.stop();
                            }
                        });

                Singleton.getmInstance(getApplicationContext()).addToRequestQueue(imageRequest);
            }
        });
    }
}

Это мой класс Singleton:

package in.jimmytrivedi.volleytutorials;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class Singleton {

    private static Singleton mInstance;
    private RequestQueue requestQueue;
    private Context context;

    private Singleton(Context ctx) {
        context = ctx;
        requestQueue = getRequestQueue();
    }

    public RequestQueue getRequestQueue() {
        if(requestQueue==null) {
            requestQueue = Volley.newRequestQueue(context.getApplicationContext());
        }
        return requestQueue;
    }

    public static synchronized Singleton getmInstance(Context context) {
        if(mInstance==null) {
            mInstance = new Singleton(context);
        }
        return mInstance;
    }

    public<T> void addToRequestQueue(Request<T> request) {
        requestQueue.add(request);
    }
}

Это Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/getServerImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/getServerData"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-230dp"
        android:text="Get Server Image" />

    <TextView
        android:id="@+id/serverResponse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="163dp"
        android:text="Server Response"
        android:textSize="30dp"
        tools:layout_editor_absoluteX="155dp"
        tools:layout_editor_absoluteY="44dp" />

    <Button
        android:id="@+id/getServerData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="230dp"
        android:text="Get Server Data" />

    <ImageView
        android:id="@+id/serverImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"/>


</RelativeLayout>

И это ошибка, показапуск приложения:

09-20 20:39:22.619 22972-22972/in.jimmytrivedi.volleytutorials I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
        at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
        at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
        at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
        at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
        at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
        at void in.jimmytrivedi.volleytutorials.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:37)
        at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6729)

При нажатии любой из кнопок:

`09-20 20:40:45.054 22972-23148/in.jimmytrivedi.volleytutorials W/System: ClassLoader referenc`ed unknown path: /system/framework/tcmclient.jar
09-20 20:40:45.057 22972-23148/in.jimmytrivedi.volleytutorials D/NetworkSecurityConfig: No Network Security Config specified, using platform default
09-20 20:40:52.618 22972-22972/in.jimmytrivedi.volleytutorials W/System.err: com.android.volley.TimeoutError
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:173)
09-20 20:40:52.619 22972-22972/in.jimmytrivedi.volleytutorials W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

Мой сервер Internet и Xampp работает нормально, а также работает 2-х строчный URL.Я тестирую на реальном устройстве.Также могу ли я использовать синглтон-класс для stringrequest и imagerequest?

...