A small project needs to read the database, and there is a server at hand. In some cases, I still want to read the data remotely. In case of exceptions
Logcat exception:
Singleclientconnmanager (411): Invalid use of singleclientconnmanager: connection still allocated.
Make sure to release the connection before allocating another one.
Request time failed: java.net. socketexception: address family not supported by Protocol
The basic idea is to use a button to listen to events and use httpclient for interaction. <For more information, click here: View>
Core code:
PHP code:
<? PHP
$ Array =Array('Title' => 'name', 'value' => 'domain ');
Json_encode ($ array );
?>
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //TextView homeContext=new TextView(this); setContentView(R.layout.tel); Button btn = (Button)findViewById(R.id.getPhpJson); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EditText edit = (EditText)findViewById(R.id.typeId); Log.i(Tag, "eeeeeeee"); String url = "http://www.test.com:80/an/index.php?type=1"; getServerJsonDataWithNoType(url,edit); } }); }-----------------------------------------------------------------------------------------------------------------public void getServerJsonDataWithNoType(String url,EditText editText) { int res = 0; HttpClient client = new DefaultHttpClient(); StringBuilder str = new StringBuilder(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpRes = client.execute(httpGet); httpRes = client.execute(httpGet); res = httpRes.getStatusLine().getStatusCode(); if(res == 200) { BufferedReader buffer = new BufferedReader(new InputStreamReader(httpRes.getEntity().getContent())); for(String s = buffer.readLine(); s != null ; s = buffer.readLine()) { str.append(s); } //String out = EntityUtils.toString(httpRes.getEntity().getContent(), "UTF-8"); //StringBuilder sb = new StringBuilder() Log.i(Tag,str.toString()); try { //JSONObject json = new JSONObject(str.toString()).getJSONObject("content"); JSONObject json = new JSONObject(str.toString()); String title = json.getString("title"); Log.i(Tag,title); int id = json.getInt("id"); String value = json.getString("value"); Log.i(Tag,value); editText.setText("Title:" + title + " ID:" + id + " Value:" + value); } catch(JSONException e) { Log.i(Tag, e.getLocalizedMessage()); //buffer.close(); e.printStackTrace(); } } else { Log.i(Tag, "HttpGet Error"); } } catch(Exception e) { Log.i(Tag, "Exception"); } }
Solution:
The URL is not spelled out. The local virtual server is used, and the local Android IP address corresponds to 10.0.2.2. Therefore, the URL should be spelled as follows: <it seems that the default IIS website can only be used for interaction, if anyone knows how to use hosts to intercept DNS resolution, please leave a message>
String url = "http: // 10.0.2.2/tell/index. php? Type = 1 ";
In this way, there is no problem!
PS: IIS requires two steps to load the PHP module:
1. Load php5isapi. dll
2. IIS> corresponding website> Home directory> Configuration> ing to load the corresponding php5isapi. dll
Bytes ------------------------------------------------------------------------------------------------------------------------------