android,与PHP通信,返回JSON
小項(xiàng)目需要讀取數(shù)據(jù)庫(kù),剛好手頭有服務(wù)器,處于某些考慮,還是想遠(yuǎn)程讀數(shù)據(jù),所遇異常
Logcat異常:
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
基本思路就是,通過(guò)一個(gè)按鈕監(jiān)聽(tīng)事件,利用HttpClient來(lái)實(shí)現(xiàn)交互。<關(guān)于更多HttpClient點(diǎn)擊此處:穿梭查看>
上核心代碼:
?
PHP代碼:
<?php
$array=array('title'=>'name','value'=>'doooger');
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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubEditText 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");}}
?
解決辦法:
url沒(méi)拼對(duì),用的是本地的虛擬服務(wù)器,android的本地ip對(duì)應(yīng)的是10.0.2.2,所以u(píng)rl應(yīng)該如下拼寫:<貌似只能用IIS的默認(rèn)網(wǎng)站進(jìn)行交互,如果誰(shuí)知道怎么用hosts攔截DNS解析用域名訪問(wèn)的方法歡迎留言>
String url = "http://10.0.2.2/tell/index.php?type=1";
這樣,就沒(méi)有問(wèn)題了!
PS:IIS加載PHP模塊需要兩步:
1、加載php5isapi.dll
2、iis->對(duì)應(yīng)網(wǎng)站->主目錄->配置->映射加載對(duì)應(yīng)的php5isapi.dll
------------------------------------------------------------------------------------------------------------------------------
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lonelyDog/archive/2012/06/05/2537479.html
總結(jié)
以上是生活随笔為你收集整理的android,与PHP通信,返回JSON的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 独角天使换昭君魔力+奥比亏赚
- 下一篇: [建议] GCC 新手入门【转】