2017-03-05 9 views
-3

私は、アプリケーションの構築を行っています。このアプリケーションは、URLからJSONデータをダウンロードし、アンドロイドアプリケーションで表示します。かなり新しいことです。JSONデータをAndroid Appにダウンロード

jsonデータのダウンロードに問題がありますか?誰かに助けてもらえますか?

パブリッククラスMainActivityは、あなたがしたいことを行うには、いくつかの方法がありますAppCompatActivity {

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

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    ListView EmployeesList = (ListView) findViewById(R.id.EmployeesList); 




    HttpURLConnection urlConnection; 
    InputStream in = null; 
    try { 
     // the url we wish to connect to 
     URL url = new URL("http://localhost:8000/"); 
     // open the connection to the specified URL 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     // get the response from the server in an input stream 
     in = new BufferedInputStream(urlConnection.getInputStream()); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // covert the input stream to a string 
    String response = convertStreamToString(in); 
    // print the response to android monitor/log cat 
    System.out.println("Server response = " + response); 

} 


public String convertStreamToString(InputStream is) { 
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); 
    return s.hasNext() ? s.next() : ""; 
} 

}

+0

どのようなトラブル/エラーですか?どのようなコードを使用しますか?私たちは推測することはできません。 – sudo

+0

ダウンロードしたらどういう意味ですか、ウェブサービスを利用するのですか? – Mehdi

+0

@Mehdi私はすでにウェブサービスを作成しています。私はアンドロイドスタジオを使ってURLからデータをダウンロードしたいと思います。 – GRattab

答えて

1

を拡張します。私の提案は、JSON構文解析を実行するためにvolley(最も良い、現代的、そしてかなり良い)ライブラリを使用することです。

Here is short example which will provide to you an idea how to do this.

これはあなたがあなたの目標を達成するためのアクションを実行できるかだけ考えであることに注意してください!

また、POJO、HTTPリクエスト処理、その他のライブラリでこれらのアクションを実行できます。さまざまな方法があります。それをGoogleとあなたに最適なものを選択してください。

+0

PJOのない方法はありません – GRattab

関連する問題