0

i have code in my android project that download json string from rest api , but the url inside downloaded string is invalid its become like : in some reason the url correct it's self in stackoverflow site but i mean the dashes become like this in log cat : (https : // www. example.com this is screenshot from log cat : url screenshot from logcat can someone help me to fix this problem , thank you in advance

this is my code that i write to retrieve json from api :

public class LoadThread extends Thread {

private PakageParams pakage = new PakageParams() ;

private void preparePakage(){
    pakage.setUri("https://example/api/v2.1/search?");
    pakage.setMethod("GET");
    pakage.setParams("lat","25.800693");
    pakage.setParams("lon", "55.976199");
}

public void run() {


    if(!isInterrupted()){

        String json = loadData();
        if(json != null ){
            EventBus.getDefault().post(new OnjsonLoaded(json));
        } else {
            EventBus.getDefault().post(new OnjsonLoaded("caanot load json"));
        }

    }


}

private String loadData(){
    preparePakage();
    String uri = pakage.getUri();
    uri += pakage.getEncodedParams();
    BufferedReader reader =  null ;
    HttpURLConnection conn = null ;
    try {
        URL url = new URL(uri);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("user-key",API_KEY_STRING);

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null ;
        while((line = reader.readLine())!= null){
            sb.append(line );
        }
        return sb.toString();

    } catch (MalformedURLException e){
        return null ;
    } catch (IOException e) {
        e.printStackTrace();
        if(reader !=null ){
            try {
                reader.close();
                conn.disconnect();
            } catch (IOException e1) {
                e1.printStackTrace();
                return e1.getMessage() ;
            }
        }
        return e.getMessage() ;
    }


}

}

2

1 Answer 1

0

Don't worry it will work fine when you will call it. (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.