【问题标题】:Android - JSON to textview using asynctaskAndroid - JSON 到 textview 使用 asynctask
【发布时间】:2018-02-04 10:31:41
【问题描述】:

我正在创建一个电影目录应用。我有一个问题,我想得到detailmovie(来自电影ID)。我从listview 发送id 电影,我已经创建了DetailActivity 并且我测试了电影标题,但它不起作用。我能做什么?

这是我的代码

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 库。

【问题讨论】:

  • 究竟是什么不工作?此外,您永远不想显示您的 API 密钥。
  • 我的 Log.d 没有显示,并且 textview 没有改变
  • 您在 logcat 中遇到错误吗?
  • 不,我没有收到错误,但是我的代码不起作用
  • 我建议您将您的应用程序置于调试模式并逐步执行以确定失败的位置。

标签: java android json android-asynctask


【解决方案1】:

你不能那样接受 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多