我需要使用这个 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();
}
}
我也在开发中以同样的情况尝试此代码,但不幸的是,此代码不起作用,我无法在此应用程序代码上看到此按钮代码或其他内容。
有人可以帮助我,如何使用一个按钮和两个编辑文本来做到这一点?
需要有关使用上述 API 执行此操作的一些帮助。
我假设
public void decode(View view)
从按钮被调用点击?
我建议在AsyncTask中进行网络调用(并管理进度对话框)。例子:
使用 AsyncTask 进行安卓网络连接
如何使用AsyncTask在Android中进行后台工作时显示ProgressDialog?
您可以在 AsyncTask 的 onPostExecute 中更新第二个 EditText(您可以使用 TextView)。