如何在android中提取和显示字段
How to extract and display field in android
请在下方查找Main.java代码
public class Main extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.my_button).setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
Button b = (Button)findViewById(R.id.my_button);
b.setClickable(false);
new LongRunningGetIO().execute();
}
private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://191.166.1.88:2000");
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
protected void onPostExecute(String results) {
if (results!=null) {
EditText et = (EditText)findViewById(R.id.my_edit);
et.setText(results);
}
Button b = (Button)findViewById(R.id.my_button);
b.setClickable(true);
}
}
}
我从REST API获取JSON数据,如下所示
{"Tm":{"Time": "Mon Nov 20 12:45:59 IST 2015"},"Name":
{"Host": "u1", "IP": "190.166.169.137"},"Speed":{"cpu":
2494.259033203125}}
如何只提取主机的"u1"的Nama,并在android中的文本框中显示。我是新手,请帮助
将您的doInBackground更改为这一点,也使doInBackbackground的返回类型从String返回到JSONObject。
try {
URL url = new URL("http://191.166.1.88:2000");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
System.out.println(connection.getResponseMessage());
responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String responseData = "", data="";
while ((data = reader.readLine()) != null){
responseData += data + "'n";
}
jsonResponse = new JSONObject(responseData);
} else {
//Not Success
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return jsonResponse;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return jsonResponse;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return jsonResponse;
}
return jsonResponse;
以及对这个的onPostExecute
@Override
protected void onPostExecute(JSONObject result) {
if(result != null) {
JSONObject name =result.getJSONObject("Name");
String host = name.getString("Host");
}
}
相关文章:
- 如果选择单选按钮,则显示字段
- 将单击事件添加到显示字段
- 如何在android中提取和显示字段
- yii2:依赖于Action的条件显示/显示字段
- ExtJs组合框显示字段编码
- 用于显示字段的工具提示和显示 Jquery 验证中的错误的工具提示器
- 根据输入值隐藏/显示字段
- 实时表编辑其他 mySQL 表中的显示字段
- ExtJS 5:显示字段绑定性能
- EXT JS :在组合框中,如何为不在存储中的记录设置显示字段
- 带有模板的显示字段
- ExtJS 4 组合框在表单绑定上显示值字段而不是显示字段
- web2py控件窗体,选择选项时显示字段
- HTML表单根据所选内容切换隐藏/显示字段
- 隐藏和显示字段集
- 如何制作html5和java脚本中所需的显示字段?下面是我的代码
- Javascript:阻止浏览器在按下向下键时显示字段的输入历史记录
- 反应 ― 显示字段但获取对象
- 如何根据编辑页上复选框中的值隐藏和显示字段
- 在asp.net中选择单选按钮值时隐藏/显示字段