[英]Android - JSON to textview using asynctask
I'm creating a Movie Catalog app. 我正在创建一个电影目录应用程序。 I have a problem, I want to get detailmovie
(from the movie id). 我有一个问题,我想获取detailmovie
(从电影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. 我从获取的listview
发送ID电影,我已经创建了DetailActivity
并测试了电影标题,但是它不起作用。 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. 我正在使用themoviedb
API。 loopj library for the listview. listj的loopj库。
You can not take JSON like that. 您不能像这样使用JSON。 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);
首先,您必须以字符串形式读取来自Web服务的响应,然后使用JSONObject = new JSONObject(string);
to convert it to a JSONObject. 将其转换为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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.