0

This is my first time using Volley.My program is to create a simple request.I am trying it in my real device.I have tried retrieving image from the server but still its the same error everytime.

greetings.php

<?php
    echo "Hello from server";
?>

MainActivity.java

String server_url = "http://192.168.43.150/greetings.php";
TextView textView;
Button button;

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

    textView = (TextView) findViewById(R.id.textView);
    button = (Button) findViewById(R.id.button);
    button.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) {
                            textView.setText(response);
                            requestQueue.stop();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    textView.setText("Something went wrong!!!");
                    error.printStackTrace();
                    requestQueue.stop();
                }
            });
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                    10000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
            ));
            requestQueue.add(stringRequest);
        }
    });
}
}

My 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="com.example.vishal.trycreche.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:text="Server Response"
    android:textSize="20sp"
    android:id="@+id/textView"/>
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:text="Click here"/>

</RelativeLayout>

My logcat

03-11 19:59:05.018 2216-2532/com.example.vishal.trycreche I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err:com.android.volley.TimeoutError
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:169)
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err:at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)

I couldn't find any solution to this. Please show me where m i doing wrong... Please help me with this...Thank you in advance

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
Vishal
  • 69
  • 1
  • 8

1 Answers1

0

I think the problem is due to this either url or network connectivity

String server_url = "http://192.168.43.150/greetings.php";

change the above url and check whether you are getting error

SABDAR SHAIK
  • 651
  • 1
  • 6
  • 13