2017-08-26 44 views
0

我正在創建一個電影目錄應用程序。我有一個問題,我想獲得detailmovie(從電影ID)。我從listview發送ID電影,我已經創建了DetailActivity,我測試了電影標題,但它不起作用。我能做什麼?Android - JSON到使用asynctask的textview

這裏是我的代碼

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

} 

我使用themoviedb API。用於listview的loopj庫。

+0

什麼是不工作?此外,你永遠不想顯示你的API密鑰。 – Cheticamp

+0

我的Log.d沒有顯示,, textview沒有改變 –

+0

你在logcat中出現錯誤? – Cheticamp

回答

0

你不能拿這樣的JSON。因此你的代碼拋出這個

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

首先你必須閱讀字符串的形式從web服務響應,然後用JSONObject = new JSONObject(string);將它轉化成一個JSONObject。例如

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

它對你有用嗎? –