How to use Serverresponse in LiveData?

  android, android-livedata, php, response

in my MainActivity im getting a String with a Serverresponse. Ive tried a lot to use this string like split it up as an array and then use it in livedata to get it in my recyclerview but it dont work what ever i tried.
It drives me crazy.

My response code i get the user location send them to a server filter them on serverside and get a response as String back how I could use this string with something like an observer or live data or how could i get a response as an array not a string to work with it?
I dont know how to fix my problem and didnt find something after searching the internet for hours.

My MainActivity Code

public void onLocationChanged(Location location) {



            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            getAddressString(latitude, longitude);
            Log.d(TAG, "onLocationChanged: postition" + " latitude " + latitude + " longitude" + longitude);
            final String lat1 = String.valueOf(latitude);
            final String lng1 = String.valueOf(longitude);

            Log.d(TAG, "onLocationChanged: String lat= "+ lat1 + "String lng= " + lng1 +    Config.KEY_EMP_LAT   +  Config.KEY_EMP_LNG ) ;

            class AddCoord extends AsyncTask<Void, Void, String> {
                public String res;
                ProgressDialog loading;


                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    Log.d(TAG, "onPreExecute: "+ responsevalue);
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    Log.d(TAG, "onPreExecute: "+ responsevalue);
                    
                }

                @Override
                protected String doInBackground(Void... v) {
                    HashMap<String, String> params = new HashMap<>();
                    params.put(Config.KEY_EMP_LAT, lat1);
                    params.put(Config.KEY_EMP_LNG, lng1);
                    RequestHandler rh = new RequestHandler();
                    res = rh.sendPostRequest(Config.URL_ADD, params);
                    Log.d(TAG, "doInBackground: "+ params);
                    Log.d(TAG, "doInBackground: SENDED!");
                    Log.d(TAG, "doInBackground: "+ res);
                    responsevalue=res;
                    return res;
                }
            }


            AddCoord ac = new AddCoord();
            ac.execute();


        }

MY RequestHandler

public class RequestHandler {

private static final String TAG = "RSHANDLER";

//Method to send httpPostRequest
//This method is taking two arguments
//First argument is the URL of the script to which we will send the request
//Other is an HashMap with name value pairs containing the data to be send with the request
public String sendPostRequest(String requestURL,
                              HashMap<String, String> postDataParams) {
    //Creating a URL
    URL url;

    //StringBuilder object to store the message retrieved from the server
    StringBuilder sb = new StringBuilder();
    try {
        //Initializing Url
        url = new URL(requestURL);

        //Creating an httmlurl connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        //Configuring connection properties
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        //Creating an output stream
        OutputStream os = conn.getOutputStream();

        //Writing parameters to the request
        //We are using a method getPostDataString which is defined below
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;
            //Reading server response
            while ((response = br.readLine()) != null){
                sb.append(response);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

public String sendGetRequest(String requestURL){
    StringBuilder sb =new StringBuilder();
    try {
        URL url = new URL(requestURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String s;
        while((s=bufferedReader.readLine())!=null){
            sb.append(s+"n");
        }
    }catch(Exception e){
    }
    return sb.toString();
}

public String sendGetRequestParam(String requestURL, String id){
    StringBuilder sb =new StringBuilder();
    try {
        URL url = new URL(requestURL+id);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String s;
        while((s=bufferedReader.readLine())!=null){
            sb.append(s+"n");
        }
    }catch(Exception e){
    }
    return sb.toString();
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));


    }

    Log.d(TAG, "getPostDataString: result "+ result);

    return result.toString();
}

}

Source: Android Questions

LEAVE A COMMENT