2017-11-14 33 views
0

Ich versuche, eine Anwendung in Android Studio zu tun, und ich habe diesen Fehler, wenn ich versuche, ein JSON-Objekt zu analysieren. Mein JSON-Objekt ist hier https://api.myjson.com/bins/sk0rvAndroid Studio [email protected]

Kann mir jemand helfen?

Der Code für die erste Aktivität

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; 
    } 

} 

Der Code für die zweite Aktivität

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; 
    } 
} 
+1

Was ist der Fehler genau? – Boukharist

+0

Wenn ich versuche, die App auf meinem Handy zu starten, habe ich den Fehler HttpResponse @ 4e0cae2, anstatt diese Informationen von meinem JSON-Objekt zu bekommen. { \t "Extrainfo": { \t \t "pretPromotie": 10, \t \t "numeFilatelist": "Georgescu Andrei" \t \t "TimbruInfo": { \t \t \t "Tara": "Germania", \t \t \t "tematica": "Diverse", \t \t \t "anAparitie": 1980, \t \t \t "pret": 17, \t \t \t "Spitze": "Stampilat" \t \t} \t} } Das ist das Problem ... Ich weiß nicht, wo der Fehler genau. –

+0

Wenn die Antwort, die ich zur Verfügung gestellt habe, funktioniert, akzeptiere sie bitte als Antwort – Boukharist

Antwort

1

es ist, weil Sie die Referenz auf das Objekt

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

drucken du sho Mache dies stattdessen

HttpEntity entity = httpResponse.getEntity(); 
String result = EntityUtils.toString(entity); 
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); 
Verwandte Themen