【发布时间】: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 时需要一些帮助。
【问题讨论】: