【问题标题】:Asking the user for a URL to receive a JSON向用户询问 URL 以接收 JSON
【发布时间】:2017-12-15 08:24:52
【问题描述】:

作为一个练习练习,我正在尝试制作一个从 URL 获取 JSON 的应用程序。

我在 stackoverflow 的其他线程中找到了以下代码,它工作得很好。我的问题是 URL 是硬编码的,我需要它是用户的输入。我应该更改/添加什么?

public class MainActivity extends AppCompatActivity {

    Button btnHit;
    TextView txtJson;
    ProgressDialog pd;

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

        btnHit = (Button) findViewById(R.id.btnHit);
        txtJson = (TextView) findViewById(R.id.tvJsonItem);

        btnHit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new JsonTask().execute("Url address here");
            }
        });
    }

    private class JsonTask extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Please wait");
        pd.setCancelable(false);
        pd.show();
    }

    protected String doInBackground(String... params) {

        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                Log.d("Response: ", "> " + line);   //here u ll get whole response..... :-) 
            }

            return buffer.toString();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pd.isShowing()){
            pd.dismiss();
        }
        txtJson.setText(result);
    }
}
}

这是我从中获取代码的线程:

Get JSON Data from URL Using Android?

【问题讨论】:

  • 尝试 volley 而不是 this 。它可以更好地帮助你。
  • 你的意思是想让用户输入url而不是硬编码的url?
  • 有人可以帮我吗? 在 StackOverflow 上不是一个有效的问题......无论如何是什么阻止你添加 EditText ?
  • 在 UI 中,在该按钮之前获取一个 EditText 并在按钮单击时写入验证,如果它是 URL,那么您可以继续,否则您显示 toast 或提醒用户输入 URL
  • @SachinBahukhandi 我不知道 volley 是什么,我猜它是一个外部库。我去看看。

标签: java android json


【解决方案1】:

在你的异步任务中创建一个构造函数

private class JSONTask extends AsyncTask<String, String, String> {
String url;
public JSONTask(String url){
this.url=url;
}

使用 url 字符串代替 params[0]

无论你在哪里调用你的异步任务,都这样做

new JSONTask(textView.getText()).execute()

这应该可以解决。

否则你可以直接在后台变量params中使用do。

【讨论】:

  • 这有点奏效。我最终做的是创建一个EditText,然后在你说的那一行中使用new JsonTask().execute(editTextUrl.getText().toString()); 谢谢!
【解决方案2】:

所以问题是您使用的是TextViewTextView 不接收输入。

EditText 会。

进行这些更改:

TextView txtJson;

在您的 OnCreate 中更改:

txtJson = (EditText) findViewById(R.id.tvJsonItem);
btnHit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new JsonTask().execute(txtJson.getText());
        }
});

现在在您的xml 文件中将Button 更改为EditText

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 2011-11-26
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多