2016-11-06 12 views
0

私のアプリケーションを実行すると、JSONデータが表示されません。 それはこのJSONオブジェクトが動作していない/接続中

Toast.makeText(this, "can't execute http request", Toast.LENGTH_LONG).show();

public class IsstatusActivity extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.isstatus); 
     TextView latCor = (TextView) findViewById(R.id.Coordinates); 
     latCor.setText(getJson()); 

    } 

    public String getJson() { 
     String quoteUrl = "https://api.wheretheiss.at/v1/satellites"; 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet(quoteUrl); 

     httpget.setHeader("Content-type", "application/json"); 

     InputStream inputStream = null; 
     String result = null; 
     String aJsonString1 = null; 
     try { 
      HttpResponse response = httpclient.execute(httpget); 

      HttpEntity entity = response.getEntity(); 

      inputStream = entity.getContent(); 
      // json is UTF-8 by default 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      }  TextView latCor = (TextView) findViewById(R.id.Coordinates); 


      result = sb.toString(); 
      JSONObject jObject = new JSONObject(result); 
      aJsonString1 = jObject.getString("latitude"); 
      latCor.setText(aJsonString1); 

     } catch (Exception e) { 
      Toast.makeText(this, "can't execute http request", Toast.LENGTH_LONG).show(); 
     } finally { 
      try { 
       if (inputStream != null) inputStream.close(); 
      } catch (Exception squish) { 
      } 
     } 

     return aJsonString1; 
    } 
} 
+2

あなたは例外を投稿できますか?おそらく、あなたがメインスレッドでHTTPコールをしようとしているからでしょう。 – vkislicins

+0

あなたはスタックトレースを投稿して詳細を教えてください。 –

答えて

0

を言い、それは例外を見ずに言うのは難しいですが、私の推測では、あなたがあなたのmainスレッドのHTTP呼び出しを行うにしようとしている例外をスローします許可されていません。 Androidが長時間実行しているタスクをどのように処理するかを理解しておくことをお勧めします。あなたは便利な次のリソースを見つけるかもしれない:

https://www.youtube.com/watch?v=jtlRNNhane0 https://developer.android.com/training/basics/network-ops/index.html http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

0

vkislicinsは正しいようです。

また、ネットワーキングの問題の後でも、https://api.wheretheiss.at/v1/satellitesから返される応答はJSONArrayですが、JSONObjectとして解析しようとすると例外が発生します。あなたは応答を解析する必要があります

String latitude = null; 
JSONArray jsonArray = new JSONArray(result); 
if (jsonArray.length() > 0) { 
    latitude = jsonArray.optJSONObject(0).optString("latitude"); 
} 
関連する問題