2016-04-27 8 views
0

ローカルREST APIを使用するためにAndroidを使用してモバイルクライアントを作成しようとしています eclipseでrest serverを作成しましたが、問題なく実行されています 私はクライアントを作成しようとしていますアンドロイドスタジオでも実行されていますが、ボタンをクリックしても何も起こっていません。AndroidでREST APIを使用する方法

public class MainActivity extends AppCompatActivity { 
    private Button btnChercher; 
    private TextView tvJson; 

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

     btnChercher = (Button) findViewById(R.id.btnchercher); 
     tvJson = (TextView) findViewById(R.id.tvJson); 
     btnChercher.setOnClickListener(new View.OnClickListener() { 
              @Override 
              public void onClick(View v) { 
               new JsonTask().execute("http://localhost:8080/membres"); 
              } 


             } 
     ); 
    } 


    public class JsonTask extends AsyncTask<String,String,String>{ 

     @Override 
     protected String doInBackground(String... params) { 
      HttpURLConnection connection=null; 
      BufferedReader reader=null; 

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

       connection.connect(); 

       InputStream stream = connection.getInputStream(); 

       reader = new BufferedReader(new InputStreamReader(stream)); 

       StringBuffer buffer = new StringBuffer(); 

       String line =""; 

       while ((line=reader.readLine())!=null){ 
        buffer.append(line); 
       } 

       String finalJson = buffer.toString(); 

       JSONObject parentObject = new JSONObject(); 
       JSONArray parentArray = parentObject.getJSONArray(""); 
       JSONObject finalObject = parentArray.getJSONObject(0); 

       String nomMembre = finalObject.getString("nomMembre"); 

       return nomMembre+"\n"; 





      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } finally { 
       if(connection!=null){ 
        connection.disconnect(); 
       } 

       if(reader!=null){ 
        try { 
         reader.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 


      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      tvJson.setText(result); 
     } 
    } 
}` 
+0

[Retrofit](http://square.github.io/retrofit/)を使用すると、RESTクライアントを作成する作業が大幅に簡略化されます。 – chRyNaN

答えて

1

この例では、URLは"http://localhost:8080/membres"です。それは意図的なのでしょうか?あなたのAndroidアプリケーションはあなたのWebサーバーを実行していますか?

おそらくそうではありません。 localhostはお使いのマシン自体を指しているため、"http://localhost:8080/membres"はコンピュータ上で動作します。お使いのAndroidアプリのアクセスあなたのREST APIを作るために

は、次のいずれかを実行する必要があります。

  • は、サーバーのIPとlocalhostを交換してください。これはあなたのAndroidアプリがあなたのサーバーと同じネットワーク上にあることを必要とします。
  • Herokuのような場所にアプリケーションをホストします。公開されているURLにlocalhost:8080を置き換えてください。

他のオプションもありますが、最も簡単です。

+0

別のオプション(同じネットワークに接続しないようにする)はルータのポートをリダイレクトしますが、常に同じ方向に接続するには固定IPが必要です。 –

+0

あなたの兄弟ggmathurとお考えになる場合 –

+0

私のIPアドレスで置き換えると実行されますが、Eclipseのコンソールを見ると何も表示されません.Hibernateの要求を見ることができます。私はJSONコード –

関連する問題