Issue
I'm creating a Movie Catalog app. I have a problem, I want to get detailmovie
(from the movie id). I send the id movie from the listview
I get, I already created the DetailActivity
and I test get the movie title but it doesn't work. What can i do?
here's my code
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);
}
}
}
I'm using themoviedb
API.
loopj library for the listview.
Solution
You can not take JSON like that. And hence your code throws this
08-26 23:26:27.871 20145-20862/? W/System.err: org.json.JSONException: No value for original_title
first you have to read response from the webservice in the form of string and then use JSONObject = new JSONObject(string);
to convert it to a JSONObject. For example
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();
}
Answered By - Ashvin Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.