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;
}
}
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.