Home Android Studio networking.httpResponse@4e0cae2
Reply: 1

Android Studio networking.httpResponse@4e0cae2

Mada O
1#
Mada O Published in 2017-11-14 23:41:16Z

I'm trying to do an application in Android Studio and I have this error when I'm trying to parse a JSON object. My JSON object is here https://api.myjson.com/bins/sk0rv

Can someone help me?

The code for the first activity

public class HttpConnection extends AsyncTask<String, Void, HttpResponse> {

    URL url;
    HttpURLConnection connection;


    @Override
    protected HttpResponse doInBackground(String... params) {

        try {
            url=new URL(params[0]);
            connection=(HttpURLConnection) url.openConnection();

            InputStream inputStream=connection.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            BufferedReader reader=new BufferedReader(inputStreamReader);
            String line=reader.readLine();
            reader.close();
            inputStreamReader.close();
            inputStream.close();
            connection.disconnect();
            return  getHttpResponseFromJson(line);


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    private HttpResponse getHttpResponseFromJson(String json) throws JSONException {
        if(json==null){
            return null;
        }
        JSONObject object=new JSONObject(json);
        ExtraInfo extraInfo=getExtraInfoFromJson(object.getJSONObject("ExtraInfo"));

        return new HttpResponse(extraInfo);
    }




    private ExtraInfo getExtraInfoFromJson(JSONObject object) throws JSONException {
        if(object==null)
        {
            return null;
        }

        ExtraInfo extraInfo=new ExtraInfo();
        extraInfo.setPretPromotie(object.getInt("pretPromotie"));
        extraInfo.setNumeFilatelist(object.getString("numeFilatelist"));

        JSONObject timbruInfo=object.getJSONObject("TimbruInfo");

        if(timbruInfo!=null)
        {
            TimbruInfo timbru=new TimbruInfo();

            timbru.setTara(timbruInfo.getString("taraOrigine"));
            timbru.setTematica(timbruInfo.getString("tematica"));
            timbru.setAnAparitie(timbruInfo.getInt("anAparitie"));
            timbru.setPret(timbruInfo.getInt("pret"));
           timbru.setTip(timbruInfo.getString("tip"));

        }
        return extraInfo;
    }

}

The code for the second activity

public class PromotiiActivity extends AbstractActivity{

    private static String URL="https://api.myjson.com/bins/sk0rv";

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



        HttpConnection connection= new HttpConnection(){
            @Override
            protected void onPostExecute(HttpResponse httpResponse) {
                super.onPostExecute(httpResponse);

                if(httpResponse!=null)
                {
                    Toast.makeText(getApplicationContext(), httpResponse.toString(), Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "is Empty", Toast.LENGTH_LONG).show();
                }
            }
        };
        connection.execute(URL);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem item = menu.findItem(R.id.app_menu_promotii);
        item.setVisible(false);
        return true;
    }
}
Boukharist
2#
Boukharist Reply to 2017-11-15 13:44:22Z

it's because you print the reference to your object

Toast.makeText(getApplicationContext(), httpResponse.toString(), Toast.LENGTH_LONG).show();

you should do this instead

HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300187 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO