Click here to Skip to main content
16,016,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to Android platform. I couldn't get the response result from the HTTP GET request. Need a guidance on where i went wrong.

public class Window extends Service {
public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

HttpRequest request = new HttpRequest();
        String json = request.execute("url").toString();

        txtPackageName.setText(json); //txtPackageName - is a TextView
}


My HttpRequest File

public class HttpRequest extends AsyncTask<String,Void, JSONArray> {

    @Override
    protected JSONArray doInBackground(String... params) {
        URL url;
        HttpURLConnection urlConnection = null;
        JSONArray response = new JSONArray();

        try {
            url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();

            String responseString = readStream(urlConnection.getInputStream());
            Log.v("CatalogClient", responseString);
            response = new JSONArray(responseString);
            /*
            if(responseCode == HttpStatus.SC_OK){
                String responseString = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", responseString);
                response = new JSONArray(responseString);
            }else{
                Log.v("CatalogClient", "Response code:"+ responseCode);
            }*/

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(urlConnection != null)
                urlConnection.disconnect();
        }

        return response;
    }

    private String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }


}
Posted
Updated 5-Dec-15 2:39am
v2
Comments
Suvendu Shekhar Giri 4-Dec-15 11:40am    
Can you share the error details ?
Hari-CodeBlogger 5-Dec-15 2:52am    
i got the correct data on responseString. The JSONArray has to be Sorted.
response = new JSONArray(responseString);
Thank you for your time and effort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900