【问题标题】:Send the string to url with Android and get response in EditText使用 Android 将字符串发送到 url 并在 EditText 中获得响应
【发布时间】:2015-06-16 16:08:48
【问题描述】:

我需要使用这个api,

"http://sconysoft.com/api/md5.php?q=" 

做这样的应用程序:

https://play.google.com/store/apps/details?id=com.sconysoft.md5encoderdecoder&hl=en

实际上,我在 Android 应用程序中有一个带有两个 EditText 的按钮,我只需要将第一个字符串发送到这个 api,并且 如果这个哈希编码(在 url 中 ) 存在,在Edit Text2 中显示。 这是应用编解码器类的上方:

public class Codec {



    public String decode(String md5) throws Exception {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(new HttpGet("http://sconysoft.com/api/md5.php?q=" + md5));
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            JSONObject jo = new JSONObject(br.readLine());

            if(jo.getString("md5").equals(""))
                throw new Exception();

            return jo.getString("word");
        } catch (Exception e) {
        }

        throw new Exception("Can't decode given md5");
    }

}

主要:

public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) 
          getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
public void decode(View view) {
        final EditText etWord = (EditText)findViewById(R.id.edit1);
        final EditText etMd5 = (EditText)findViewById(R.id.edit2);

        if(!isNetworkAvailable()) {
            etWord.setText("Network unavailable");
            return;
        }

        final ProgressDialog pd = ProgressDialog.show(view.getContext(),"Waiting for Server", "It should take a few seconds");

        Thread th = new Thread() {
            @Override
            public void run() {
                try {
                    Codec cd = new Codec();
                    String md5 = etMd5.getText().toString();
                    try {
                        final String word = cd.decode(md5);
                        runOnUiThread(new Runnable() {
                            public void run() {
                                etWord.setText(word);
                                pd.dismiss();
                            }
                        });
                    } catch (final Exception e) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                etWord.setText(e.getMessage());
                                pd.dismiss();
                            }
                        });
                    }
                } catch(Exception e) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            pd.dismiss();
                        }
                    });
                }
            }
        };
        th.start();
    }
}

我也在开发过程中尝试了这个代码,但不幸的是,这个代码不起作用,我在这个应用代码上看不到这个按钮代码或其他东西。

谁能帮助我,如何用一个按钮和两个 EditTexts 来做到这一点?

在使用上述 API 时需要一些帮助。

【问题讨论】:

    标签: android json


    【解决方案1】:

    我假设

    public void decode(View view)
    

    从按钮 onClick 调用?

    我建议在 AsyncTask 中进行网络调用(并管理进度对话框)。例子:

    Using AsyncTask for android network connection

    How to use AsyncTask to show a ProgressDialog while doing background work in Android?

    您在 AsyncTask 的 onPostExecute 中更新第二个 EditText(您可以使用 TextView)。

    【讨论】:

      猜你喜欢
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2019-02-06
      相关资源
      最近更新 更多