2017-08-26 12 views
0

ムービーカタログアプリを作成しています。私は問題がある、私はdetailmovie(映画のIDから)を取得したい。私はlistviewからidムービーを送ってきます。私はすでにDetailActivityを作成していますが、私は映画タイトルを取得しますが動作しません。私に何ができる?私はthemoviedb APIを使用していAndroid - JSONからasynctaskを使用したテキストビュー

public class DetailActivity extends AppCompatActivity { 

TextView txt_title,txt_detail,txt_rate; 
ImageView img_posterd; 

public static String API_KEY = "f00e74c69ff0512cf9e5bf128569f****"; 

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

    Intent i = getIntent(); 
    String idmov = i.getStringExtra("idmovie"); 

    String url = "https://api.themoviedb.org/3/movie/"+idmov+"?api_key="+API_KEY+"&language=en-US"; 
    txt_title = (TextView) findViewById(R.id.tv_title_detail); 
    new GetMovieTask(txt_title).execute(url); 

} 

private class GetMovieTask extends AsyncTask<String, Void, String>{ 

    TextView txt_title; 

    public GetMovieTask(TextView txt_title) { 
     this.txt_title = txt_title; 

    } 

    @Override 
    protected String doInBackground(String... strings) { 
     String title = "UNDEFINED"; 

     try { 
      URL url = new URL(strings[0]); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      JSONObject object = new JSONObject(); 
      title = object.getString("original_title"); 

      urlConnection.disconnect(); 
      Log.d("titlenya", title); 

     }catch (IOException | JSONException e){ 
      e.printStackTrace(); 
     } 
     return title; 

    } 

    @Override 
    protected void onPostExecute(String original_title) { 
     txt_title.setText("Ti : " +original_title); 
    } 
} 

} 

は、ここに私のコードです。 listviewのloopjライブラリ。

+0

正確には動作しません。また、APIキーを表示したくない場合もあります。 – Cheticamp

+0

私のLog.dは表示されず、textviewは変更されていません –

+0

logcatでエラーが発生しましたか? – Cheticamp

答えて

0

JSONをそのまま使用することはできません。それゆえ、あなたのコードは、文字列の形式でWebサービスからの応答を読み、その後、JSONObjectに変換するJSONObject = new JSONObject(string);を使用する必要が最初にこの

08-26 23:26:27.871 20145-20862/? W/System.err: org.json.JSONException: No value for original_title 

をスローします。たとえば、

try { 
       URL url = new URL(strings[0]); 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

       int responseCode = urlConnection.getResponseCode(); 
       Log.i(TAG, "POST Response Code: " + responseCode); 

       //Takes data only if response from WebService is OK 
       if (responseCode == HttpURLConnection.HTTP_OK) { 
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
        String inputLine; 
        StringBuilder response = new StringBuilder(); 

        //Stores input line by line in response 
        while ((inputLine = in.readLine()) != null) { 
         response.append(inputLine); 
        } 
        in.close(); 

        //get data in JSON 
        JSONObject object = new JSONObject(response.toString()); 
        title = object.getString("original_title"); 

       urlConnection.disconnect(); 
       Log.d("titlenya", title); 

      }catch (IOException | JSONException e){ 
       e.printStackTrace(); 
+0

それはあなたのために働いていますか? –

関連する問題