Javascript 如何在android中提取和显示字段
请在下面输入Main.java代码Javascript 如何在android中提取和显示字段,javascript,java,android,node.js,rest,Javascript,Java,Android,Node.js,Rest,请在下面输入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).
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);
}
}
}
如何只提取主机名为“u1”的Nama,并在android的文本框中显示。我是新手,请帮助将doInBackground更改为this,并将doInBackground的返回类型从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;
而onpost执行到这个
@Override
protected void onPostExecute(JSONObject result) {
if(result != null) {
JSONObject name =result.getJSONObject("Name");
String host = name.getString("Host");
}
}
我添加了Json jar文件,但也显示了很多错误。您不需要Json jar。有什么错误?找不到Json对象
@Override
protected void onPostExecute(JSONObject result) {
if(result != null) {
JSONObject name =result.getJSONObject("Name");
String host = name.getString("Host");
}
}