android,與PHP通信,返回JSON


小項目需要讀取數據庫,剛好手頭有服務器,處於某些考慮,還是想遠程讀數據,所遇異常

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

基本思路就是,通過一個按鈕監聽事件,利用HttpClient來實現交互。<關於更多HttpClient點擊此處:穿梭查看>

上核心代碼:

 

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() {

            @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");
            }
    
    }

 

解決辦法:

url沒拼對,用的是本地的虛擬服務器,android的本地ip對應的是10.0.2.2,所以url應該如下拼寫:<貌似只能用IIS的默認網站進行交互,如果誰知道怎么用hosts攔截DNS解析用域名訪問的方法歡迎留言>

String url = "http://10.0.2.2/tell/index.php?type=1";

這樣,就沒有問題了!

PS:IIS加載PHP模塊需要兩步:

1、加載php5isapi.dll

2、iis->對應網站->主目錄->配置->映射加載對應的php5isapi.dll

------------------------------------------------------------------------------------------------------------------------------

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM